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

时间:2015-02-19 00:40:11

标签: php cakephp cakephp-3.0

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

$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

这是一个cakephp 3错误吗?或者是我误导了$table->alias()

1 个答案:

答案 0 :(得分:3)

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