How do I delete an entity from symfony2

Symfony is smart and knows how to make the find() by itself : public function deleteGuestAction(Guest $guest) { if (!$guest) { throw $this->createNotFoundException(‘No guest found’); } $em = $this->getDoctrine()->getEntityManager(); $em->remove($guest); $em->flush(); return $this->redirect($this->generateUrl(‘GuestBundle:Page:viewGuests.html.twig’)); } To send the id in your controller, use {{ path(‘your_route’, {‘id’: guest.id}) }}

Difference between ObjectManager and EntityManager in Symfony2?

ObjectManager is an interface and EntityManager is its ORM implementation. It’s not the only implementation; for example, DocumentManager from MongoDB ODM implements it as well. ObjectManager provides only the common subset of all its implementations. If you want your form type to work with any ObjectManager implementation, then use it. This way you could switch … Read more

Doctrine 2 OneToMany Cascade SET NULL

You should add the option onDelete=”SET NULL” in the annotation of your entity Publication like this: class Publication { /** * @ORM\ManyToOne(targetEntity=”Teacher”, inversedBy=”publications”) * @ORM\JoinColumn(name=”teacher_id”, referencedColumnName=”id”, onDelete=”SET NULL”) */ protected $teacher; } This modification is registered in the table declaration itself. Hence, you need a database migration or a schema change for this to take … Read more

Doctrine: cascade=”remove” vs orphanRemoval=true

The basic difference between them is: When using the orphanRemoval=true option Doctrine makes the assumption that the entities are privately owned and will NOT be reused by other entities. If you neglect this assumption your entities will get deleted by Doctrine even if you assigned the orphaned entity to another one. Say your User has … Read more

Understanding Doctrine Cascade Operations

persist & remove You are correct about cascade={“persist”} meaning that persisting entity A, Doctrine will also persist all B entities in the Collection. You are also correct about cascade={“remove”} meaning that removing entity A, Doctrine will also remove all B entities in the Collection. But I doubt you would ever want to use this on … Read more

Deprecation: Doctrine\ORM\Mapping\UnderscoreNamingStrategy without making it number aware is deprecated

In most cases I would just answer this sort of question with a comment but I suspect other developers might run into this issue. I poked around a bit and could not find any explicit documentation on this issue. Perhaps because the DoctrineBundle is under the control of the Doctrine folks and not the Symfony … Read more

How to re-save the entity as another row in Doctrine 2

Try cloning and add the following method to your entity public function __clone() { $this->id = null; } You may need to detach the entity before persisting it. I don’t have my dev machine handy to test this right now. $f = clone $e; $em->detach($f); $em->persist($f); $em->flush(); Update Just tried using a simple SQLite demo. … Read more

Doctrine – A new entity was found through the relationship

I had the same problem and it was the same EntityManager. I wanted to insert an object related ManyToOne. And I don’t want a cascade persist. Example : $category = $em->find(“Category”, 10); $product = new Product(); $product->setCategory($category) $em->persist($product); $em->flush(); This throws the same exception for me. So the solution is : $category = $em->find(“Category”, 10); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)