A strange AR error

There are some code:




//AAA HAS_ONE BBB

AAA::model()->with(array('BBB'=>array(

            'select'=>'BBB.price',

            'joinType'=>'LEFT JOIN',

            'params' => array(':country'=>'USA',':count'=>1),

            'on'=>'BBB.country=:country AND BBB.count=:count'))

    )->findByAttributes(array('anAttr'=>123));



that gives an error




PHP Error[2]: Invalid argument supplied for foreach()

    in file /yii/db/ar/CActiveFinder.php at line 826

#0 /yii/db/ar/CActiveFinder.php(862): CJoinElement->populateRecord()

#1 /yii/db/ar/CActiveFinder.php(804): CJoinElement->populateRecord()

#2 /yii/db/ar/CActiveFinder.php(437): CJoinElement->runQuery()

#3 /yii/db/ar/CActiveFinder.php(68): CJoinElement->find()

#4 /yii/db/ar/CActiveRecord.php(1356): CActiveFinder->query()

#5 /yii/db/ar/CActiveRecord.php(1519): AAA->query()

#6 /home/usr/l.net/protected/commands/ServiceCommand.php(108): AAA->findByAttributes()



But the most strange is in the log. Log shows correct SQL query was executed. I see the query and can execute manually and it works. But the error does not allow me work with returned data.

I think the problem is in "LEFT JOIN".

"joinType=>INNER JOIN" gives right query and returns the data to the code, no errors.

And if I don’t use ‘on’ but use ‘condition’ instead - it also works correct!

Any ideas?

This seems to be happening while populating the ‘child records’.

My first guess is that you have to replace ‘on’ with ‘condition’ in order to let the regular join condition operate.

Further, given what you are doing, have a look at my RelatedSearchBehavior extension.

It will allow you to write your search like this:


AAA::model()->bbb_country('USA')->bbb_count(1)->anAttr(123)->find();

once you have defined ‘bbb_country’ and ‘bbb_count’ as relation values.

Problem is solved. I haven’t defined any primary keys in the database for “BBB” table