Symfony Bulk Update with Propel

By marc • May 12th, 2008 • Category: Symfony

Here is a quick example of how to do a bulk update with Propel within symfony. This is based on this code snippet on the symfony site. I thought it was extremely useful, so I wanted to post it as an easy reference for myself and others.

Suppose we had a query which looked like this:

  1.  
  2. UPDATE articles SET STATUS = 0 WHERE user_id = 10;

Here is how we would translate it into symfony/propel terms:

  1.  
  2. // set the select condition criteria
  3. $c = new Criteria();
  4. $c->add(ArticlePeer::USER_ID, 10);
  5.  
  6. // set the update criteria
  7. $update = new Criteria();
  8. $update->add(ArticlePeer::STATUS, 0);
  9.  
  10. // we need the connection for update, so get default connection
  11. $con = Propel::getConnection();
  12.  
  13. // finally, do the update
  14. BasePeer::doUpdate($c, $update, $con);

We are just creating the first criteria to find all the objects that we want to select and then update. Then creating the second one that specifies which values we are updating. Easy!

The main thing to notice is that we are using the ‘BasePeer’ class, so you need to make sure that you explicitly pass in a connection, otherwise it won’t work as expected!

marc is a Web Developer in Los Angeles, CA. He likes to receive comments :)
All posts by marc

One Response »

  1. Thanks, needed it, googled it, applied it)
    I wish only propel’s syntax become more simple

Leave a Reply