【发布时间】:2016-11-24 12:29:13
【问题描述】:
我正在使用 cakePHP3 查询生成器使用以下查询从两个表中获取记录,其中我想要 table1 中的所有列和 table2 中的选定列:
$this->loadModel('Table1');
$Table1 = $this->Table1->find('all', array('fields' => array('Table1.*'),'conditions'=>$conditions,'order'=>array('Table1.id'=>'DESC')))->contain(['Table2']);
但我收到以下错误
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 'AS `Table1__*` FROM Table1 Table1 LEFT JOIN Table2 Table2 ON ' at line 1
我是 CakePHP3 的新手。
【问题讨论】:
-
请参考正确的语法:
$this->Table1->find('all', array( 'joins' => array( array( 'table' => ‘table1’, 'alias' => ‘table’, 'type' => 'INNER', 'conditions' => array( ‘table1.id = table2.id’ ) ) ), 'conditions' => $conditions, 'fields' => array(‘table1.*’, ‘table2.field1, table2.field2’), )); -
使用你的代码我得到这个错误:
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 'ASTable1__*, Table2.field1, Table2.field2 AS Table2__field1, Prod' at line 1 -
看起来 CakePHP 无法处理
.*