使用 symfony/propel 进行增量更新

发布于 2024-08-13 18:29:13 字数 221 浏览 5 评论 0原文

我想使用 Propel ORM 在 symfony 中执行这样的查询:

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

我知道 Propel API 可以让我为给定记录的列设置先前固定的值,但我绝对不想先检索列值在发出更新查询之前,因为存在并发访问。

请问我怎样才能实现这个目标?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-08-20 18:29:13

这适用于 Symfony 1.3/1.4:

在 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();
}

使用:

AdsPeer::incrementHits($id);

HTH

Mike

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

笑咖 2024-08-20 18:29:13

如果您愿意,可以在 propel 中执行选择使用 SQL

下面是一个示例:

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);    
  }

您也可以使用 update 来尝试此操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文