简体   繁体   中英

Delete DB entry on Symfony

Lets imagine we have this kind of code:

//...
$item = $xyz->findOneBy(array('type'=>'abc'));
$em->remove($item);
$second = $xyz->findOneBy(array('type'=>'abc'));
//...

Does this $second get any values from DB ? My goal is to get $second as an empty list. But i'm not sure do i have to do $em->flush(); right after remove() or not. If i do, then of course it works, but then my script execution time goes too high.

Methods remove() or add() does not perform any operation on database - flush() does. So you will still get the same results of findOneBy as before remove() - you have to call flush() to perform INSERT , UPDATE or DELETE on DB

Flush should commit the changes to the database. The entitymanager should however be aware of the changes that have not been flushed yet.

You can have a virtual dataset from your entitymanager while not flushing.

UPDATE From Doctrine documentation: Just like persist, invoking remove on an entity does NOT cause an immediate SQL DELETE to be issued on the database. The entity will be deleted on the next invocation of EntityManager#flush() that involves that entity. This means that entities scheduled for removal can still be queried for and appear in query and collection results.

After an entity has been removed its in-memory state is the same as before the removal, except for generated identifiers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM