1

In CakePHP 2 we can use aliases in updateAll method, but in CakePHP 3 (tested 3.4.9) cannot using alias?

$this->_table->updateAll([
    $this->_table->getAlias() . '.deleted' => Time::now()
], [
    $this->_table->getAlias() . '.' . $this->_table->getPrimaryKey() => $entity->{$this->_table->getPrimaryKey()}
]);

If Your table is products and alias is Products it's works, but doesn't work if Your table is cart_products and alias is CartProducts

The SQL should looks like:
UPDATE cart_products AS CartProducts SET... WHERE CartProducts.id = ...
instead of
UPDATE cart_products SET... WHERE CartProducts.id = ...

BTW: code above is from my behavior.

kicaj
  • 2,881
  • 5
  • 42
  • 68
  • "_doesn't work_" is not a proper problem description. Even if the problem might be obvious for people that know the CakePHP internals, please always be as specific as possible as to what _exactly_ happens, and what _exactly_ you'd expect to happen instead. I guess the problem is that the resulting query will contain aliases in the `UPDATE` clause, but not in the `WHERE` clause? – ndm Jul 15 '17 at 16:58
  • I added SQL code... The SQL looks like `UPDATE cart_products SET ... WHERE CartProducts.id = ...`. There are missing `AS CartProducts`after `cart_products` – kicaj Jul 15 '17 at 17:33
  • Where do you see that generated SQL? I would expect the `WHERE` clause to not have aliases (for reasons that I can explain in answer later on), and a quick test with 3.4.9 shows exactly that for me, aliases in the `SET` clause, but none in the `WHERE` clause. – ndm Jul 15 '17 at 17:51

1 Answers1

1

You can't use aliases in updateAll() queries. The updateAll() function doesn't support joins, so all fields can be used unaliased.

Mark Story
  • 1,269
  • 6
  • 12