Deleting Doctrine Entries

I am trying to delete an entry in Doctrine, but I do not know why it is not being deleted.

Here is my code:

function del_user($id) { $single_user = $entityManager->find('Users', $id); $entityManager->remove($single_user); $entityManager->flush(); } 

Plus: how can I perform an echo request to find out what is going on here?

+6
source share
6 answers

This is an old question and there seems to be no answer yet. For reference, I leave this here for more details. You can also check the doctrine documentation.

To delete an entry, you need (if you are in your controller):

 // get EntityManager $em = $this->getDoctrine()->getManager(); // Get a reference to the entity ( will not generate a query ) $user = $em->getReference('ProjectBundle:User', $id); // OR you can get the entity itself ( will generate a query ) // $user = $em->getRepository('ProjectBundle:User')->find($id); // Remove it and flush $em->remove($user); $em->flush(); 

Using the first method to get the reference is usually better if you just want to delete the object without first checking whether it exists or not, because it will not query the database and will only create a proxy object that you can use to delete your entity.

If you want to make sure that this identifier matches the actual entity first, then the second method is better because it will query the database for your entity before trying to delete it.

+9
source

In the Silex route, I like this if it helps someone:

 $app->get('/db/order/delete', function (Request $request) use ($app) { ... $id = $request->query->get('id'); $em = $app['orm.em']; //or wherever your EntityManager is $order = $em->find("\App\Entity\Orders",$id); //your Entity if($order){ try{ $em->remove($order); $em->flush(); } catch( Exception $e ) { return new Response( $e->getMessage(), 500 ); } return new Response( "Success deleting order " . $order->getId(), 200 ); }else{ return new Response("Order Not Found", 500); } } 
+2
source

try var_dump() your $single_user . If it is " null ", does it not exist?

Also check if "Users" is a valid Entity name (without a namespace?), And does $ id refer to the user's PK?

If you want to view executed queries, check your mysql / sql / ... log or look in Doctrine \DBAL\Logging\EchoSQLLogger

0
source

First you need a repository.

 $entityManager->getRepository('Users')->find($id); 

instead

 $single_user = $entityManager->find('Users', $id); 

The 'Users' String is the name of the Users repository in the doctrine (it depends on whether you use Symfony, Zend, etc.).

0
source

Are you testing your essence as a wonderful comment annotation?

 cascade={"persist", "remove"}, orphanRemoval=true 
0
source

First, you may need to check if "Users" is your complete class. If you do not check and update it to the name of your class with namespace information.

Ensure that the object returned by find () is not null or false and is an instance of the entity class before calling EM remove ().

As for your other question, instead of returning the doctrine of SQL, I just use my database (MySQL) to register all queries (from the moment it was created).

0
source

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


All Articles