Wednesday, February 18, 2015

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

Hi,

To avoid the "Column 'xxx' in where clause is ambiguous" error when making queries involving multiple tables sharing the same name for a given column, I now systematically include $this->alias() in where conditions :

$table->find()->where([$table->alias() . '.foo' => 'bar']);


Is this considered a good practice? 

For some reasons I ignore, doing so leads to an error when using update instead of find for tables that contains an underscore. Example:

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

will throw the error:
Column not found: 1054 Unknown column 'BlogPosts.id' in 'where clause'

for the prepared request:
UPDATE blog_posts SET BlogPosts.title = :c0 WHERE BlogPosts.id = :c1

Is this a cakephp 3 bug? Or is it me who is misusing $table->alias()?

Thx!


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: