在CakePHP上手动加入

时间:2013-06-20 11:06:55

标签: mysql cakephp cakephp-2.0 cakephp-appmodel

我正在尝试手动加入,因为我希望得到比CakePHP更好的查询结果。

这是我的控制器的编码

$this->ClinicWishList->contain();
$this->set('recordSets', $this->ClinicWishList->find('all', array(
  'fields' => array('ClinicWishList.*', 'ApplicantIssue.issue_desc', 'User.nickname', 'User.role'),
  'joins' => array(
    array(
      'table' => 'applicant_issues',
      'alias' => 'ApplicantIssue',
      'type' => 'left outer',
      'foreignKey' => false,
      'conditions' => array('ClinicWishList.issue_id' => 'ApplicantIssue.id')
    ),
    array(
      'table' => 'users',
      'alias' => 'User',
      'type' => 'left outer',
      'foreignKey' => false,
      'conditions' => array('ApplicantIssue.user_id' => 'User.id')
    )),
    'conditions' => array('ClinicWishList.user_id' => $this->Auth->user('id')),
    'order' => 'ClinicWishList.created DESC'
  )));

CakePHP就像这样产生了SQL。

enter image description here

没有错误,但我从这样的连接表中获得了空结果。

enter image description here

我复制了SQL并试图在GUI工具上执行,我收到了这个警告。

enter image description here

如何修改CakePHP中的编码以获得类似

的SQL

enter image description here

而不是

enter image description here

请帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

您应该更改此行:

'conditions' => array('ClinicWishList.issue_id' => 'ApplicantIssue.id')

为:

'conditions' => array('`ClinicWishList`.`issue_id` = `ApplicantIssue`.`id`')

并更改此行:

  'conditions' => array('ApplicantIssue.user_id' => 'User.id')

为:

  'conditions' => array('`ApplicantIssue`.`user_id` = `User`.`id`')