views:

343

answers:

2

Hi, I would like execute a query like this one in symfony using the Propel ORM:

UPDATE ADS SET HITS=HITS+1 WHERE ID=10;

I know that Propel API can let me set a previously fixed value for a column of a given record, but I definitely don't want to retrieve the column value first before issuing an update query since there are concurrent access.

Please, how could I achieve this?

Thanks.

A: 

If you like you can do a Select using SQL in propel.

Here's an example:

class BookPeer extends BaseBookPeer {
  .
  .
  .
  /**
   * Get just the Books that have not been reviewed.
   * @return array Book[]
   */
  function getUnreviewedBooks() {
    $con = Propel::getConnection(DATABASE_NAME);

    // if not using a driver that supports sub-selects
    // you must do a cross join (left join w/ NULL)
    $sql = "SELECT books.* FROM books WHERE ".
           "NOT EXISTS (SELECT id FROM review WHERE book_id = book.id)";

    $stmt = $con->createStatement();
    $rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM);

    return parent::populateObjects($rs);    
  }

You can try this with update as well.

Ngu Soon Hui
+1  A: 

This works in Symfony 1.3/1.4:

in AdsPeer:

public static function incrementHits($id)
{
  $con = Propel::getConnection(AdsPeer::DATABASE_NAME);
  $query = 'UPDATE hits SET hits + 1 WHERE id = '.$id;
  sfContext::getInstance()->getLogger()->crit($query);
  $stmt = $con->prepare($query);
  $rs = $stmt->execute();
}

To use:

AdsPeer::incrementHits($id);

HTH

Mike

Mike Crowe