How to change the sequence of 'joins' in CakePHP?

前端 未结 4 951
萌比男神i
萌比男神i 2020-12-03 20:21

I have the problem with the sequence of joins. The similar problem was in another question Manipulating Order of JOINS in CakePHP. The answer was to use Containable behavior

4条回答
  •  没有蜡笔的小新
    2020-12-03 20:30

    To those who has similar problems but in relations a-la $belongsTo, to have correct order you should set it correctly.

    For example when you have such code:

    var $belongsTo = array(
        'Vacancy',
        'Applicant' => array(
            'className' => 'Person',
        ),
        'Recruiter' => array(
            'className' => 'Person',
        ),
        'Company' => array(
            'conditions' => array('Company.id = Vacancy.company_id'),
        ),
    );
    

    But in the result always receive results where Vacancy always joins last, you should do juts simple thing: add those "Vacancy" model not as array value, but as a key=>value, as others:

    var $belongsTo = array(
        'Vacancy' => array(), // Just add empty array here -- all magic is here :)
        'Applicant' => array(
            'className' => 'Person',
        ),
        'Recruiter' => array(
            'className' => 'Person',
        ),
        'Company' => array(
            'conditions' => array('Company.id = Vacancy.company_id'),
        ),
    );
    

    Now all will be in straight order: Vacancy, Applicant, Recruiter & only then Company.

提交回复
热议问题