开发者

incremental update with symfony/propel

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, b开发者_JS百科ut 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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜