How to use a findBy method with comparative criteria

The class Doctrine\ORM\EntityRepository implements Doctrine\Common\Collections\Selectable API. The Selectable interface is very flexible and quite new, but it will allow you to handle comparisons and more complex criteria easily on both repositories and single collections of items, regardless if in ORM or ODM or completely separate problems. This would be a comparison criteria as you just … 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

Auto quote reserved words with Doctrine 2

This was an issue I raised a while back with the Doctrine team. https://github.com/doctrine/doctrine2/issues/2409 The ticket was closed with the comment: You have to manually escape characters with @Column(name=”`integer`”) So I guess you’d need to deal with any reserved keywords in your annotations

Symfony 2: Creating a service from a Repository

DEPRECATION WARNING: No more factory_service and factory_method. This is how you should do it since Symfony 2.6 (for Symfony 3.3+ check below): parameters: entity.my_entity: “AppBundle:MyEntity” services: my_entity_repository: class: AppBundle\Repository\MyEntityRepository factory: [“@doctrine”, getRepository] arguments: – %entity.my_entity% The new setFactory() method was introduced in Symfony 2.6. Refer to older versions for the syntax for factories prior to … Read more

How to setup table prefix in symfony2

Having just figured this out myself, I’d like to shed some light on exactly how to accomplish this. Symfony 2 & Doctrine 2.1 Note: I use YML for config, so that’s what I’ll be showing. Instructions Open up your bundle’s Resources/config/services.yml Define a table prefix parameter: Be sure to change mybundle and myprefix_ parameters: mybundle.db.table_prefix: … 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

Doctrine2 ORM does not save changes to a DateTime field

The DateTime instances returned by ExampleBundle\Entity\User#getServiceExpiresAt() are the same objects stored in the entity itself, which breaks encapsulation. The UnitOfWork in Doctrine ORM applies strict comparison for changesets, which basically means that in the case of properties of entities containing objects, if the object instance hasn’t changed, the ORM does not detect a change. In … 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.