在CakePHP中更新行+1 [英] Update a row +1 in 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屋!