πter
πter

Reputation: 2217

How does persist work in Symfony?

I am new to Symfony, and I could not find an answer on the official site. Can I use one variable to persist more then once ? For example:

for ($i = 0; $i < count($allArea); $i++) {
             $area = new Area();
             $area->setValue($i);
             $em->persist($area);
}
$em->flush();

Upvotes: 1

Views: 2705

Answers (1)

Artur Yukhatov
Artur Yukhatov

Reputation: 190

The persist($area) call tells Doctrine to "manage" the $area object. This does not cause a query to be made to the database.

When the flush() method is called, Doctrine looks through all of the objects that it's managing to see if they need to be persisted to the database.

So basically in this case use flush() inside the loop or, if you want to use it once outside - create new 'area' obejects and do use the same $area.

Upvotes: 1

Related Questions