2

I have collections of clients, and every collection can contain many clients. This PHP code loops collections, and clients inside every collection. And saves the client to the database.

foreach ($collections as $key => $collection) {
      foreach ($collection as $k => $client) {
                 $name = $client['name'];
                 //...
                 $clientObj = new Client();
                 $clientObj->setName($name);
                 //..
                 $clientObj->save();
      }
}

What I want to do, is to group every collection in one Mysql query, then go to the next collection. Because the previous code executes one query per client, And for performance, we need one query per collection.

How can we do that?

3
  • 1
    Have already tried this? Commented Mar 25, 2015 at 10:18
  • 1
    Thank you so much my dear for the reference , I'm gonna give it a try Commented Mar 25, 2015 at 10:20
  • 1
    Woow, thanks my dear, that works perfectly... You can add your comment as an answer, to make it best answer ! Commented Mar 25, 2015 at 10:30

1 Answer 1

2

Add each record to a Doctrine_Collection the call save() on the collection object.

 * Saves all records of this collection and processes the 
 * difference of the last snapshot and the current data

As Example:

$collection = new Doctrine_Collection('client');
$collection->add($client1);
$collection->add($client2);
$collection->save();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.