Doctrine query builder using inner join with conditions

I’m going to answer my own question. innerJoin should use the keyword “WITH” instead of “ON” (Doctrine’s documentation [13.2.6. Helper methods] is inaccurate; [13.2.5. The Expr class] is correct) no need to link foreign keys in join condition as they’re already specified in the entity mapping. Therefore, the following works for me $qb->select(‘c’) ->innerJoin(‘c.phones’, ‘p’, … Read more

The differences between GeneratedValue strategies

Check the latest doctrine documentation Here is a summary : the list of possible generation strategies: AUTO (default): Tells Doctrine to pick the strategy that is preferred by the used database platform. The preferred strategies are IDENTITY for MySQL, SQLite and MsSQL and SEQUENCE for Oracle and PostgreSQL. This strategy provides full portability. SEQUENCE: Tells … Read more

Is there a way to specify Doctrine2 Entitymanager implementation class in Symfony2?

After Doctrine 2.4 (Doctrine 2.4 release) you need to use decorator for this. Do not extend EntityManager directly. First you need to implement you own entity manager decorator that extends Doctrine\ORM\Decorator\EntityManagerDecorator (like @Dana) But you can’t just change doctrine.orm.entity_manager.class to your new decorator because EntityManagerDecorator requires EntityManagerInterface in it’s constructor: public function __construct(EntityManagerInterface $wrapped) You … Read more

The EntityManager is closed

My solution. Before doing anything check: if (!$this->entityManager->isOpen()) { $this->entityManager = $this->entityManager->create( $this->entityManager->getConnection(), $this->entityManager->getConfiguration() ); } All entities will be saved. But it is handy for particular class or some cases. If you have some services with injected entitymanager, it still be closed.

How to get a one-dimensional scalar array as a doctrine dql query result?

PHP < 5.5 You can use array_map, and since you only have on item per array, you can elegantly use ‘current’ as callback, instead of writing a closure. $result = $em->createQuery(“SELECT a.id FROM Auction a”)->getScalarResult(); $ids = array_map(‘current’, $result); See Petr Sobotka’s answer below for additional info regarding memory usage. PHP >= 5.5 As jcbwlkr’s … Read more

Symfony2, Dynamic DB Connection/Early override of Doctrine Service

Combined, these two postings helped me solve my own very similar problem. Here is my solution, maybe it is useful for someone else: <?php namespace Calitarus\CollaborationBundle\EventListener; use Symfony\Component\HttpFoundation\Request; use Doctrine\DBAL\Connection; use Exception; use Monolog\Logger; class DatabaseSwitcherEventListener { private $request; private $connection; private $logger; public function __construct(Request $request, Connection $connection, Logger $logger) { $this->request = $request; … Read more

Doctrine 2 mysql FIELD function in order by

Jeremy Hicks, thanks for your extension. I didn`t know how to connect your function to doctrine, but finally i find answer. $doctrineConfig = $this->em->getConfiguration(); $doctrineConfig->addCustomStringFunction(‘FIELD’, ‘DoctrineExtensions\Query\Mysql\Field’); I need FIELD function to order my Entities that i select by IN expression. But you can use this function only in SELECT, WHERE, BETWEEN clause, not in ORDER … Read more

tech