Cakephp 3:使用“$这->;alias()";在“;其中“;条件


Cakephp 3: use of "$this->alias()" in "where" conditions

为了避免在进行涉及多个表共享同一列名称的查询时出现Column 'xxx' in where clause is ambiguous错误,我现在系统地将$this->alias()包含在where条件中,如下所示:

$table->find()->where([$table->alias() . '.field' => 'value']);

这被认为是一种好的做法吗?

由于我忽略的某些原因,当对包含下划线的表使用updateAll而不是find时,这样做会导致错误。示例:

$table = TableRegistry::get('BlogPosts');
$table->updateAll(
    [$table->alias() . '.title' => 'new title'],
    [$table->alias() . '.id'    => 1]
);

将抛出错误:Column not found: 1054 Unknown column 'BlogPosts.id' in 'where clause'用于准备好的查询UPDATE blog_posts SET BlogPosts.title = :c0 WHERE BlogPosts.id = :c1

这是一个cakepp3错误吗?还是我滥用$table->alias()

您不应该对updateAll的条件进行别名。原因是该方法只涉及一个表,因此不需要别名