Strange problem with relations

Hi to all.

I’m new with Yii, and have following problem.

I have Property and Booking models related one to many.

this is code snippet of relation rules in Property and Booking models, respectivly


return array(

'bookings' => array(self::HAS_MANY, 'Booking', 'property_id',

                'alias' => 'b',

                'condition' => "b.is_active=1"

            ),

);

and


return array(

	'property' => array(self::BELONGS_TO, 'Property', 'property_id'),

);

but if I use


$property->bookings

to get bookings for that property I get this error message


<h1>CException</h1>

<p>Property "Property.property_id" is not defined. (C:\xampp\yii\framework\base\CComponent.php:131)</p><pre>#0 C:\xampp\yii\framework\db\ar\CActiveRecord.php(144): CComponent->__get('property_id')

#1 C:\xampp\yii\framework\db\ar\CActiveFinder.php(685): CActiveRecord->__get('property_id')

#2 C:\xampp\yii\framework\db\ar\CActiveFinder.php(522): CJoinElement->applyLazyCondition(Object(CJoinQuery), Object(Property))

#3 C:\xampp\yii\framework\db\ar\CActiveFinder.php(167): CJoinElement->lazyFind(Object(Property))

#4 C:\xampp\yii\framework\db\ar\CActiveRecord.php(269): CActiveFinder->lazyFind(Object(Property))

#5 C:\xampp\yii\framework\db\ar\CActiveRecord.php(142): CActiveRecord->getRelated('bookings')

#6 C:\xampp\htdocs\uStay.net\protected\models\Property.php(711): CActiveRecord->__get('bookings')

...



This kind use of relations, is working for other relations defined in Property model in same way, but this one isn’t

if I create Property object like this


$property = Property::model()->with('bookings')->findByPk($pk);

than i don’t have this error.

property_id is composite key in table bookings, may that be a problem? :-/

Thanks in advance

If I remove property_id from composite key and make it regular foreign key, problem solved :)

Only stays that Yii doesn’t support composite keys in relations. Hope Yii 2.0 will solve this issue as well. Anyway… Thanks :)

Did you try adding both keys like (id,property_id)


return array(

'bookings' => array(self::HAS_MANY, 'Booking', '(id,property_id)',

                'alias' => 'b',

                'condition' => "b.is_active=1"

            ),

);

Btw:

You can override the primaryKey() method in your AR class like this


public function primaryKey()

{

    return array('pk1','pk2');

}