【问题标题】:incremental update with symfony/propel使用 symfony/propel 进行增量更新
【发布时间】:2010-12-23 12:35:26
【问题描述】:

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

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

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

请问,我怎样才能做到这一点?

谢谢。

【问题讨论】:

    标签: php database symfony1 propel


    【解决方案1】:

    这适用于 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

    迈克

    【讨论】:

      【解决方案2】:

      如果你愿意,你可以在推进中做一个Select using 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 试试这个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-12
        • 1970-01-01
        • 2017-04-13
        • 1970-01-01
        • 2016-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多