Why does the doctrine update every object of my form?

I have a large Symfony 2 form in a huge collection (over 10,000 objects). For simple reasons, I cannot display one of thousands of objects. I show the shape of about 300 objects.

I did not find a way to filter the collection into a form and thus do the following:

$bigSetOfObjects = array( 'myObject' => $this ->getDoctrine() ->getRepository('MyObject') ->findBy(... ) ); $form = $this->createForm(new MyObjectForm(), $bigSetOfObjects); // And a little further if ($this->getRequest()->getMethod() == 'POST') { $form->bindRequest($this->getRequest()); $this->getDoctrine()->getEntityManager()->flush(); } 

Everything works great. The form displays with the correct values, and the update also works great. Data is correctly stored in the database. The problem is that Doctrine performs one update for each object , which means that the entire page contains about 300 SQL statements causing problems .

I do not understand that I am updating only a couple of form values, and not all of them. So why can't Doctrine detect updated objects and thus only update those objects in the database?

Is there something I'm doing wrong? Did I forget?

+4
source share
1 answer

By default, Doctrine will detect changes to managed objects based on properties by properties. If the properties have not been changed, it should not fulfill the update request for it. You can verify that the form does not accidentally change anything.

However, you can change how the doctrine determines that the object has changed by changing the tracking policy. Since you work with a large number of objects, you can change the tracking policy DEFERRED_EXPLICIT . Using this method, you would call:

 $em->persist($object); 

for the objects you want to update. You will need to implement your own logic to determine if an object should be saved.

+7
source

All Articles