0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cakephp2でテーブルのエイリアス名を予約語にした際、findがエラーになる

Posted at

findのconditionsにて独自queryを使用した際、テーブルのエイリアス名に予約語を使用したらエラーになった。

サンプルコード以下に示す。

###テストコード

test.php
$option = array('conditions' => 'Customer.id IN(select Customer.id from customers as Customer INNER JOIN orders as `order` on Customer.id = order.customer_id)');

$test = $Customer->find('count', $option);

###結果

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Order ON `Customer`.`id` = `Order`.`customer_id` '

因みに、サブクエリ部分をquery()で実行すると問題なく処理が通る。

$this->query(select Customer.id from customers as Customer INNER JOIN orders as `order` on Customer.id = order.customer_id);

###原因
findのconditionsに設定したものは自動的に識別子を付与してくれるらしく、その際に一旦全ての識別子を除外するらしい。

###結論
その設定を無効化することもできるが、現実として難しいため素直にエイリアス名を変更したほうがよいだろう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?