【问题标题】:Add up value using database field value with savefield in cakephp使用数据库字段值和 cakephp 中的 savefield 将值相加
【发布时间】:2011-11-04 08:38:33
【问题描述】:

我的问题很简单,但通过搜索引擎很难找到答案。

我只是想更新数据库中的一个字段,使用该字段的旧值添加另一个值。我目前正在使用以下内容:

$this->Advertisement->saveField('total_views', '(total_views + 1)', false);

但这给了我下一个查询:

UPDATE `advertisement` SET `total_views` = '(total_views +1)', `modified` = '2011-08-26 10:44:58' WHERE `advertisement`.`id` = 16

这是错误的,应该是:

UPDATE `advertisement` SET `total_views` = (total_views +1), `modified` = '2011-08-26 10:44:58' WHERE `advertisement`.`id` = 16

问题在于它将(total_views +1) 放在引号之间。

有人知道如何让它工作吗?

【问题讨论】:

    标签: sql cakephp


    【解决方案1】:

    我发现对于类似的问题,我可以通过以下方式解决它,

    $this->Advertisement->updateAll(
    array('Advertisement.total_views = Advertisement.total_views + 1'),
    array('Advertisement.id' => 1)
    );
    

    【讨论】:

      【解决方案2】:

      以防万一其他人对此感到困惑,查询中的空格很重要。

      有效Advertisement.total_views + 1

      不起作用Advertisement.total_views+1

      【讨论】:

        【解决方案3】:
        $this->Advertisement->updateAll(
        array('Advertisement.total_views' => 'Advertisement.total_views + 1'),
        array('Advertisement.id' => 1)
        );
        

        【讨论】:

          【解决方案4】:
          $this->Advertisement->updateAll(array('Advertisement.total_views'=>'Advertisement.total_views+1'), array('Advertisement.id'=>$id));
          

          【讨论】:

            猜你喜欢
            • 2012-01-31
            • 2013-12-25
            • 1970-01-01
            • 2012-09-15
            • 2011-06-04
            • 2012-04-07
            • 2020-05-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多