【发布时间】:2015-03-22 15:13:39
【问题描述】:
更新!
我在 cakephp 方面相对较新,但对 mysql 和 php 有经验。 模型如下:
Person->Father
父亲自称人。
我根据返回“1”人的父亲的mysql查询写了以下内容 mysql:
SELECT `Father`.name,`Father`.id from persons as `Father` left join persons as `Person` on `Person`.`father_id`=`Father`.`id` where `Person`.id=1
蛋糕php
$options = array(
'fields' => array(
//'Father.name',
'Father.id',
),
'joins' => array(
array(
'conditions' => array(
'Person.father_id = Father.id',
),
'table' => 'persons',
// 'alias' => 'Person', i commented because having conflict with scaffolded model
'type' => 'left',
),
),
'conditions' => array(
'Person.id' => '1',
),
'contain' => array(
'Person','Father',
),
);
$data = $this->Person->find('first', $options);
$fatherquery=$this->Person->find('first',array('conditions'=>array('Person.id'=>$data['Father']['id'])));
为了与 mysql 相同,我添加了这个额外的行(最后一个 $father=....,但现在似乎子查询并且看起来连接不起作用)因为父亲和人不是相同的型号,如果我有
$data['Father']['name'] and $data['Person']['name'] they are not equal
顺便说一句,我已经有了解决方案,但也许我误解了一些概念。 有没有办法让mysql查询更容易?
【问题讨论】:
-
什么是 $this->find('first', $options);?你的意思是 $this->Person->find('first', $options); ?你在哪里有这个代码?在哪个控制器,模型等中。
-
是的,很抱歉它不正确,你是对的。代码在控制器中。