How to get the logged-in user’s ID in Symfony

Current Symfony versions (Symfony 4, Symfony >=3.2) Since Symfony >=3.2 you can simply expect a UserInterface implementation to be injected to your controller action directly. You can then call getId() to retrieve user’s identifier: class DefaultController extends Controller { // when the user is mandatory (e.g. behind a firewall) public function fooAction(UserInterface $user) { $userId … Read more

Limiting a doctrine query with a fetch-joined collection?

Paginate was merged with doctrine 2.2 And the new symfony2 release 2.0.10 is compatible with. Now use it like that //use Doctrine paginator use Doctrine\ORM\Tools\Pagination\Paginator; Write your query then call results like that. $query->setMaxResults($limit); $query->setFirstResult($offset); $results = new Paginator($query, $fetchJoin = true); Hope this will help you. Note: If you are using SF2 2.0.10, you … Read more

Complex WHERE clauses using the PHP Doctrine ORM

From my experience, each complex where function is grouped within parenthesis (I’m using Doctrine 1.2.1). $q->where(‘name = ?’, ‘ABC’) ->andWhere(‘category1 = ? OR category2 = ? OR category3 = ?’, array(‘X’, ‘X’, ‘X’)) ->andWhere(‘price < ?’, 10) produces the following SQL: WHERE name=”ABC” AND (category1 = ‘X’ OR category2 = ‘X’ OR category3 = ‘X’) … Read more

pass array of conditions to doctrine expr()->orx() method

I hope so, then I found this : $conditions = array(‘e.type = x’, ‘e.type = Y’, ‘e.type = N’); $orX = $qb->expr()->orX(); foreach ($conditions as $condition) { $orX->add($condition); } $qb->add(‘where’, $orX); Using @meze suggestion, you can simplify the code and replace the foreach statement with: $orX->addMultiple($conditions);

Symfony getting logged in user’s id

Current Symfony versions (Symfony 4, Symfony >=3.2) Since Symfony >=3.2 you can simply expect a UserInterface implementation to be injected to your controller action directly. You can then call getId() to retrieve user’s identifier: class DefaultController extends Controller { // when the user is mandatory (e.g. behind a firewall) public function fooAction(UserInterface $user) { $userId … Read more