Communication in YII with a non-"identifier" as a primary key

I have an Anketa model that has user_idboth a primary key and not just that id. And I want to create a HAS_ONE relation for it, so I encode it:

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 the "Property" Anketa.id "not defined". mistake.

I found the code in the 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 idfor some reason instead user_id. I do not know if this is a mistake, because other relationships work fine, but they are not as complicated as this.

Why is this happening? How can i fix this?

Thanks!

+2
source share
1 answer

PK 'on'.

function relations()
{
    return array(
        'last_experience' => array(
            self::HAS_ONE,
            'Experience',
            '',
            'on' => 'user_id=last_experience.user_id'
        ),
    );
}
+7

All Articles