find 方法中的条件
我有3个模型, Mentor, MentorAttrib, Attrib 其中 MentorAttrib 是连接表。它列出了 Mentor.id -> Attrib.id
这是我的发现
$cond = array("Mentor.is_listed"=>1);
$contain_cond = array();
$contain = array(
'MentorAttrib' => array(
'fields' => array('MentorAttrib.id' ,'MentorAttrib.attrib_id'),
'Attrib'
)
);
if(! empty($this->request->data))
{
debug($this->request->data);
//skills
if(! empty($this->request->data['bookingSkills']))
{
$cond = array('MentorAttrib.attrib_id' => $this->request->data['bookingSkills']);
}
}
$this->request->data = $this->Mentor->find('all', array(
'conditions' => $cond,
'fields' => array('Mentor.id','Mentor.first_name','Mentor.last_name','Mentor.img'),
'contain' => $contain
));
,我想按技能过滤结果。
[bookingSkills] => Array
(
[0] => 2
[1] => 4
[2] => 10
)
我得到的错误是 未找到列:1054 “where 子句”中的未知列“MentorAttrib.attrib_id”
I have 3 models,
Mentor, MentorAttrib, Attrib where MentorAttrib is a join table. it lists Mentor.id -> Attrib.id
This is my find
$cond = array("Mentor.is_listed"=>1);
$contain_cond = array();
$contain = array(
'MentorAttrib' => array(
'fields' => array('MentorAttrib.id' ,'MentorAttrib.attrib_id'),
'Attrib'
)
);
if(! empty($this->request->data))
{
debug($this->request->data);
//skills
if(! empty($this->request->data['bookingSkills']))
{
$cond = array('MentorAttrib.attrib_id' => $this->request->data['bookingSkills']);
}
}
$this->request->data = $this->Mentor->find('all', array(
'conditions' => $cond,
'fields' => array('Mentor.id','Mentor.first_name','Mentor.last_name','Mentor.img'),
'contain' => $contain
));
I want to filter the result by the skills.
[bookingSkills] => Array
(
[0] => 2
[1] => 4
[2] => 10
)
The error im getting is
Column not found: 1054 Unknown column 'MentorAttrib.attrib_id' in 'where clause'
This is the data set
http://pastebin.com/85uBFEfF
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要一个联接
默认情况下,CakePHP 只会为 hasOne 和 BelongsTo 关联创建联接 - 任何其他类型的关联都会生成另一个查询。因此,您无法通过仅注入条件并使用
contain
来根据 hasMany 或 hasAndBelongsToMany 关联中的条件过滤结果。强制使用 join
On 选项来实现您需要的查询是使用
joins
键。如文档 您还可以轻松添加联接,如下所示:这允许灵活地生成任何类型的查询。
You need a join
By default, CakePHP will only create joins for hasOne and belongsTo associations - any other type of association generates another query. As such you cannot filter results based on a condition from a hasMany or hasAndBelongsToMany association by just injecting conditions and using
contain
.Forcing a join
On option to achieve the query you need is to use the
joins
key. As shown in the docs You can also add a join, easily, like so:This allows the flexibility to generate any kind of query.