Doctrine / Symfony2 - REMOVE FROM table WHERE field = "test"

I cannot believe that I cannot find anywhere (without just including all SQL). The docs talk about adding, searching, updating, and deleting an entity, but I just don't see how to do this in pure SQL:

DELETE FROM table WHERE field = "test" 

I assume this just adds and updates, so I cannot use this:

 $product = new Product(); // etc. $em = $this->getDoctrine()->getEntityManager(); $em->persist($product); $em->flush(); 

and I don't think the delete option will do this either:

 $em->remove($product); $em->flush(); 

So can anyone point me in the right direction?

+6
source share
2 answers

Find the object and delete it.

 $em = $this->getDoctrine()->getEntityManager(); $repository = $em->getRepository('MyBundle:Product'); /** @var $product Product */ $product = $repository->findOneBy(array('field' => 'test')); $em->remove($product); $em->flush(); 
+7
source

You can create a DQL query as follows:

 $query = $repository->createQuery('DELETE FROM entity e WHERE e.id = :id'); $query->setParameter('id', $id); $query->execute(); 
+5
source

Source: https://habr.com/ru/post/950474/


All Articles