I’m having a model Anketa which has user_id as a primary key, not just id.
And I want to create a HAS_ONE relation for it so i code this:
function relations()
{
return array(
...
'last_experience' => array(
self::HAS_ONE, 'Experience', 'user_id',
'condition' => '
`signoff` = (
SELECT MAX( `signoff` )
FROM `experience` AS t2
WHERE t2.user_id = last_experience.user_id )
',
),
);
}
But when I try to get this property I get “Property “Anketa.id” is not defined.” error.
I found some code in CActiveFinder.php file that does the following:
if($this->relation instanceof CBelongsToRelation)
{
if(is_int($i))
{
if(isset($parent->_table->foreignKeys[$fk])) // FK defined
$pk=$parent->_table->foreignKeys[$fk][1];
else if(is_array($this->_table->primaryKey)) // composite PK
$pk=$this->_table->primaryKey[$i];
else
$pk=$this->_table->primaryKey;
}
$params[$pk]=$record->$fk;
}
This block returns id for some reason instead of user_id. I don’t know if it’s a bug, because other relations work fine, but they’re just not as complicated as this one.
Why does it happen? How can I fix this?
Thank you!
Try to leave the PK field empty and use the ‘on’ condition override.