doctrine-orm
How to delete rows from join-table (ManyToMany) in Doctrine?
To set cascade on doctrine level: @ORM\ManyToMany(targetEntity=”Target”, inversedBy=”inverse”, cascade={“remove”, “persist”}) More info: Doctrine2 Annotation Reference. To set cascade on mysql level: @ORM\JoinColumn(onDelete=”CASCADE”, onUpdate=”CASCADE”)
Doctrine2 $em->persist($entity) on foreach loop
From the Doctrine documentation, it says that inserts are best performed with batch. And its a development of @AlterPHP ‘s answer. You could use : $batchSize = 20; for ($i = 1; $i <= 10000; ++$i) { $car = new Car(); // … set number of wheels, but should always be to 4 right ? … Read more
Using Relationships with Multiple Entity Managers
Using different object managers (entity managers) doesn’t allow the object graphs to intersect. That case is too complex and isn’t managed by Doctrine ORM. If you need such a case, keep the object graphs disconnected by saving the identifiers of the related objects (old style) instead of a reference to them, then manually get the … Read more
Expression mysql NOW() in Doctrine QueryBuilder
In Doctrine2 you have to use one of the following instead of NOW(). This: CURRENT_TIMESTAMP() Or: … createQuery(…’WHERE x.date = :now’) ->setParameter(‘now’, new \DateTime(‘now’)) … If you want only time or only date use one of those: CURRENT_TIME() and CURRENT_DATE() Documentation can be found here.
Persisting other entities inside preUpdate of Doctrine Entity Listener
I give all the credits to Richard for pointing me into the right direction, so I’m accepting his answer. Nevertheless I also publish my answer with the complete code for future visitors. class ProjectEntitySubscriber implements EventSubscriber { public function getSubscribedEvents() { return array( ‘onFlush’, ); } public function onFlush(OnFlushEventArgs $args) { $em = $args->getEntityManager(); $uow … Read more
Doctrine2: How to set all tables to collate with UTF8
The behavior of collate has changed in doctrine: http://www.doctrine-project.org/jira/browse/DDC-2139 The collation is now set to utf8_unicode_ci if you don’t specify anything at the table level. The way to go right now is to add it to options in your table declaration: /** * @ORM\Table(options={“collate”=”utf8_swedish_ci”}) * @ORM\Entity */ This will generate the correct collation for the … Read more
Generate Symfony2 fixtures from DB?
There’s no direct manner within Doctrine or Symfony2, but writing a code generator for it (either within or outside of sf2) would be trivial. Just pull each property and generate a line of code to set each property, then put it in your fixture loading method. Example: <?php $i = 0; $entities = $em->getRepository(‘MyApp:Entity’)->findAll(); foreach($entities … Read more