has_many through on condition not working ?

Hi guys,

I don’t know what’s happening because thing does not add up so going to ask the community for help.

I have a mortgage which has 1 primary applicant and 1 or more (many) co-applicants.

The relation in my mortgage model is:

// primary applicant connection

 'primaryApplicantConnection' => array(self::HAS_ONE, 'PersonToMortgage', 'mortgage_id',

        'condition' => 'is_primary=1'



// primary applicant info

'primaryApplicant' => array(self::HAS_ONE, 'Person', 'person_id', 

	'through' => 'primaryApplicantConnection'),


// co-applicant connection

'coApplicantConnection' => array(self::HAS_MANY, 'PersonToMortgage', 'mortgage_id',

	'condition' => 'is_primary<>1 OR is_primary IS NULL'



// co-applicants' info

'coApplicants' => array(self::HAS_MANY, 'Person', 'person_id',

	'through' => 'coApplicantConnection'


In Mortgage Controller,


$primaryApplicant = $mortgage_model->primaryApplicant; // return expect result

$coApplicants = $mortgage_model->coApplicants; // the problem is here

When I want to get all the coApplicants by using $mortgage_model->coApplicants, the result also includes primary applicant.

To track the problem down, I tried this $mortgage_model->coApplicantConnection which only return co-applicant rows.

So I wonder where primary applicant coming from ? and how it ends up in $mortgage_model->coApplicants ? is it a bug ?

I dig a little bit more using awesome extension yiidebugtb to trace the actual query.

This is what’s I found out: (I already remove lengthy select …)

SELECT `coApplicants`.`person_id` 

FROM `People` `coApplicants` 

LEFT OUTER JOIN `PersonToMortgage` `coApplicantConnection` ON (`coApplicantConnection`.`person_id`=`coApplicants`.`person_id`) 

WHERE (`coApplicantConnection`.`mortgage_id`=:ypl0)

The WHERE part should include the [‘condition’ => ‘is_primary<>1 OR is_primary IS NULL’]. I will submit bug reporting

Any news on that? Seems relevant to my interests. Even though simple has many through should be working, right?