amd
amd

Reputation: 43

Yii2 update with multiple conditions

        $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'],
            ]

        );

I tried to use the following command to update a row using multiple where conditions, but it doesn't work for some reason. The documentation doesn't seem to cover this case and doesn't seem to be updated, because the update() method seems to match the updateAll method instead of the update method for some reason (maybe it was updated?). So I was wondering what was the correct way to do this.

Upvotes: 1

Views: 3995

Answers (2)

Yasin Patel
Yasin Patel

Reputation: 5731

Try This updateAll query :

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']
  ]);

Upvotes: 2

Chinmay Waghmare
Chinmay Waghmare

Reputation: 5456

You have a syntax error. Try following:

$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'],
            ]

        );

Upvotes: 3

Related Questions