Yii2更新有多个条件

时间:2017-05-16 00:19:36

标签: yii2

        $this->localdb->createCommand()
        ->update(
            $this->MYTable,
            [
                'name' => $el['new'],
                'data' => $el['data'],
            ],
            [
                'userId', $this->user,
                'product_id', $this->productId,
                'name', $el['old'],
                'created', $el['date'],
                'category', $el['cat'],
            ]

        );

我尝试使用以下命令使用多个where条件更新行,但由于某些原因它不起作用。文档似乎没有涵盖这种情况,似乎没有更新,因为update()方法似乎与updateAll方法而不是更新方法匹配由于某种原因(可能它已更新?)。所以我想知道这样做的正确方法是什么。

2 个答案:

答案 0 :(得分:1)

您遇到语法错误。请尝试以下方法:

$this->localdb->createCommand()
        ->update(
            $this->MYTable,
            [
                'name' => $el['new'],
                'data' => $el['data'],
            ],
            [
                'userId' => $this->user,
                'product_id' => $this->productId,
                'name' => $el['old'],
                'created' => $el['date'],
                'category' => $el['cat'],
            ]

        );

答案 1 :(得分:1)

尝试此updateAll查询:

MYTable::updateAll([   // field to be updated in first array
      'name' => $el['new'],
      'data' => $el['data']
  ],
  [  // conditions in second array
      'userId' => $this->user,
      'product_id' => $this->productId,
      'name' => $el['old'],
      'created' => $el['date'],
      'category' => $el['cat']
  ]);