【问题标题】:Update query in cakephp with more than one condition使用多个条件更新 cakephp 中的查询
【发布时间】:2013-01-25 05:12:24
【问题描述】:

我有一个类似的查询:

Update `pools` set status='2' where `pid`='1' and `uid`='2'

如何在 cakephp 中转换此查询?即我想在查询中传递那个 AND 条件,它应该更新包含 pid='1' 和 uid='2' 的行。

【问题讨论】:

  • 你试过什么?
  • 现在正在使用这个:$this->Pool->updateAll(array('receipt_no'=>'abc123'), array('Pool.id'=>9,'Pool. uid' => 23));但是将 'abc123' 作为字段名并引发错误

标签: php mysql cakephp


【解决方案1】:

UPDATEALL 就是你所需要的。

$this->Pool->updateAll(array('status'=>2), array('Pool.pid'=>1,'Pool.uid' => 2));

更多文档。

http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-array-conditions

如果您已经有要更新的行的 id,则无需使用 update cakephp 非常聪明,足以理解它。

例如。

如果你有Pool.id,那是记录行。

$this->data['Pool']['id'] = $pool_id;
$this->data['Pool']['uid'] = $pool_uid;
$this->data['Pool']['other_data'] = $pool_otherData;
$this->Pool->save($this->data['Pool']);

上面会自动保存 $pool_id 行。

【讨论】:

  • 我试过了,效果很好!但是,如果我像这样修改它:$this->Poo->updateAll(array('receipt_no'=>'abc123'), array('Pool.id'=>9,'Pool.uid' = > 23));它将'abc123'作为列名并给出错误作为未知列
  • 你必须使用 Model.field_name like array('Pool.receipt_no'=>'abc123')...因为 cakephp 查询根据模型名称自动创建表别名...试试这个我确定就是这样。
  • 是的,我确实使用过 ('Pool.receipt_no'=>'abc123'),但它使用 abc123 作为字段名
  • 我可以看看数据库表结构...它不可能将值参数作为列必须有问题..
  • 如果不存在则创建表pools (id bigint(11) NOT NULL AUTO_INCREMENT, pool_id bigint(11) NOT NULL, uid varchar(20) COLLATE utf8_unicode_ci NOT NULL, receipt_no varchar(255) 整理 utf8_unicode_ci 不为空,
【解决方案2】:

我想这可能有用...试试吧...

$this->Pool->updateAll(array('status'=>2), array('AND' => array('Pool.pid'=>1,'Pool.uid' => 2)));

【讨论】:

  • 不,这也不是同样的错误,它需要 'abc123' 作为字段名
  • 你的 cakephp 版本是什么??
  • Ok 终于成功了:$this->Poolcontributor->updateAll(array('receipt_no'=>"'abc123'"), array('Poolcontributor.pool_id'=>9,'Poolcontributor. user_id' => 23));我所做的是将“abc123”转换为“abc123”。 cakephp 的一个奇怪的行为
猜你喜欢
  • 2017-10-23
  • 2022-11-17
  • 2012-12-03
  • 2015-09-06
  • 2022-01-18
  • 2011-02-13
  • 2011-09-18
  • 2023-03-30
  • 2012-10-02
相关资源
最近更新 更多