在CakePHP中更新行+1 [英] Update a row +1 in CakePHP

查看:142
本文介绍了在CakePHP中更新行+1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新数据库中的一行,但没有找到一种方法,在CakePHP方法(除非我查询该行检索和更新)

  UPDATE mytable(field)VALUES(field + 1)WHERE id = 1 



在CodeIgniter中,它可以像下面这样简单:

 code> $ this-> db-> set('field','field + 1',FALSE); 
$ this-> db-> where('id',1);
$ this-> db-> update('mytable');

如何在不先查询行的情况下执行此操作,检索值,然后使用

$ p

解决方案

我不认为CakePHP有一个类似的方法在一个正常的save



但是updateAll()方法更新多个行,支持以下SQL片段:

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

第一个参数是要更新的字段/值的数组,第二个参数是条件



除此之外,我认为唯一的事情是使用:

  $ this-> Widget-> query('YOUR SQL QUERY HERE'); 

这使您可以使用原始SQL进行查询。


I am trying to update a row in the database but haven't found a way to do this in the CakePHP way (unless I query the row to retrieve and update).

UPDATE mytable (field) VALUES (field+1) WHERE id = 1

In CodeIgniter, it would have been as simple as:

$this->db->set('field', 'field+1', FALSE);
$this->db->where('id', 1);
$this->db->update('mytable');

How do I do this without querying the row first, retrieve the value, then updating the row with the information I got?

解决方案

I don't think CakePHP has a similar method for doing this in a normal save() on a single row.

But the updateAll() method, which updates multiple rows, does support SQL snippets like so:

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

The first param is an array of fields/values to be updated, and the second param are the conditions for which rows to update.

Apart from that I think the only thing is to use:

$this->Widget->query('YOUR SQL QUERY HERE');

Which lets you query with raw SQL. [EDIT: but this is not recommended as it bypasses the ORM.]

这篇关于在CakePHP中更新行+1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆