CakePHP 3 updateAll 使用别名

CakePHP 3 updateAll with alias

在 CakePHP 2 中我们可以在 updateAll 方法中使用别名,但是在 CakePHP 3(已测试 3.4.9)中不能使用别名?

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

如果您的 table 是 products 并且别名是 Products,它可以工作,但如果您的 table 是 cart_products 并且别名是CartProducts

SQL 应如下所示:
UPDATE cart_products AS CartProducts SET... WHERE CartProducts.id = ...
而不是
UPDATE cart_products SET... WHERE CartProducts.id = ...

顺便说一句:上面的代码来自我的行为。

您不能在 updateAll() 查询中使用别名。 updateAll() 函数不支持联接,因此所有字段都可以不使用别名。