开发者

cakephp: using a join for search results

开发者 https://www.devze.com 2023-02-02 01:18 出处:网络
I\'ve completely confused myself now. I have three tables: applicants, applicants_qualifications, and qualifications.

I've completely confused myself now.

I have three tables: applicants, applicants_qualifications, and qualifications.

In the index view for applicants, I have a form with a dropdown of qualifications. The results should b开发者_StackOverflow中文版e a list of applicants with that qualification.

So I need the table of applicants on the index view to be based on a join, right?

If I add this to my applicants_controller:

$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);

I get an additional sql statement at the bottom of the page, with the left outer join but the sql without the join is there too.

I think this line:

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

calls the sql statement without the join.

Looks like I need to combine the join $options with the paginate call. Is that right?

If I use the search form, I get: Unknown column 'Qualifications.qualification_id' in 'where clause'

So the page is obviously not yet 'using' my sql with the join.

Sorry - I'm still a noob. Any help appreciated...


In order to set conditions, joins, etc for your model's pagination, you must do it as follows:

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());
}

I've commented out some sample keys that you can include later on - just to give you an idea of how it works.

Hope that helps!


You can use inner join instead of left outer join.For eg.

in cource controller

$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'
                )
          );
0

精彩评论

暂无评论...
验证码 换一张
取 消