find 方法中的条件

发布于 2024-12-27 21:09:45 字数 1188 浏览 1 评论 0原文

我有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”

这是数据集 http://pastebin.com/85uBFEfF

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2025-01-03 21:09:45

您需要一个联接

默认情况下,CakePHP 只会为 hasOne 和 BelongsTo 关联创建联接 - 任何其他类型的关联都会生成另一个查询。因此,您无法通过仅注入条件并使用 contain 来根据 hasMany 或 hasAndBelongsToMany 关联中的条件过滤结果。

强制使用 join

On 选项来实现您需要的查询是使用 joins 键。如文档 您还可以轻松添加联接,如下所示:

$options['joins'] = array(
    array('table' => 'mentor_attribs',
        'alias' => 'MentorAttrib',
        'type' => 'LEFT',
        'conditions' => array(
            'Mentor.id = MentorAttrib.mentor_id',
        )
    )
);

$data = $this->Mentor->find('all', $options);

这允许灵活地生成任何类型的查询。

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:

$options['joins'] = array(
    array('table' => 'mentor_attribs',
        'alias' => 'MentorAttrib',
        'type' => 'LEFT',
        'conditions' => array(
            'Mentor.id = MentorAttrib.mentor_id',
        )
    )
);

$data = $this->Mentor->find('all', $options);

This allows the flexibility to generate any kind of query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文