【问题标题】:cakephp: using a join for search resultscakephp:使用连接搜索结果
【发布时间】:2010-12-31 13:21:37
【问题描述】:

我现在完全糊涂了。

我有三个表:applicantsapplicants_qualificationsqualifications

在申请人的索引视图中,我有一个带有资格下拉列表的表格。结果应该是具有该资格的申请人名单。

所以我需要索引视图上的申请人表基于连接,对吗?

如果我将此添加到我的申请者控制器中:

$options['joins'] = array(  
    array(  
        'table' => 'applicants_qualifications',  
        'alias' => 'q',  
        'type' => 'left outer', // so that I get applicants with no qualifications too   
        'conditions' => array('Applicant.id = q.applicant_id',)  
    )  
);
$this->Applicant->find('all', $options);

我在页面底部得到了一个额外的 sql 语句,带有左外连接,但没有连接的 sql 也在那里。

我认为这行:

$this->set('applicants', $this->paginate());

在没有连接的情况下调用 sql 语句。

看起来我需要将 join $options 与 paginate 调用结合起来。对吗?

如果我使用搜索表单,我会得到:'where 子句'中的未知列'Qualifications.qualification_id'

所以该页面显然还没有在连接中“使用”我的 sql。

对不起 - 我还是个菜鸟。任何帮助表示赞赏...

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    要为模型的分页设置conditionsjoins等,您必须按如下方式进行:

    function admin_index() {
        $this->Applicant->recursive = 0;
        $this->paginate = array(
            'Applicant' => array(
                // 'conditions' => array('Applicant.approved' => true),
                'joins' => array(
                    array(
                        'table' => 'applicants_qualifications',  
                        'alias' => 'ApplicationsQualification',  
                        'type' => 'left outer',
                        'conditions' => array('Applicant.id = ApplicationsQualification.applicant_id')  
                    )
                )
                // 'order' => array('Applicant.joined DESC')
            )
        );
        $this->set('applicants', $this->paginate());
    }
    

    我已经注释掉了一些示例键,您可以稍后将它们包含在内 - 只是为了让您了解它的工作原理。

    希望有帮助!

    【讨论】:

    • 再次感谢 RabidFire!完美运行。我喜欢 cakephp 和 stackoverflow!
    【解决方案2】:

    您可以使用内连接代替左外连接。例如。

    在源控制器中

    $cond =   array(
                array(
                    'table' => 'colleges',
                    'alias' => 'Colleges',
                    'type' => 'inner',
    
                    'conditions'=> array('Colleges.college_id = Courses.college_id')
                )
    
                ) ;
    
    
            $courses = $this->Courses->find('all',  
                array('joins' =>$cond,'conditions' => array("Courses.status ='1' AND Colleges.status='1' "),
                        'order'=>array('course_name'),
                        'fields' => array('course_id','course_name'),'group'=>'Courses.course_id'
                    )
              );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多