Yii model relationship using a field other than the primary key

I need to create a relationship based on a field that is not a primary key. Many examples of how to do this are based on a one to many and many to many relationship. I have successfully tried the suggestions from the following:

Communication in YII with no "ID" as a primary key

Yii CActiveRecord: find related data but not using primary key

Yii Relationships with non-primary keys

Joining a Yii Model Relationship (HAS_ONE)

I have the following table structure:

+------+---------+-----------+
| id   |   name  | status_id |
+------+---------+-----------+
|  1   | service1| 1         |
+------+---------+-----------+
| 2    | service2| 2         |
+------+---------+-----------+

This is my active_service table. I also have the following table

+----------+----------+---------------------+-----------+
|id        |related_id|related_text         |  text     |
+----------+----------+---------------------+-----------+
|65        |1         |ActiveServices_status|  Open     |
+----------+----------+---------------------+-----------+
|72        |2         |ActiveServices_status|  Active   |
+----------+----------+---------------------+-----------+
|102       |3         |ActiveServices_status|  Closed   |
+----------+----------+---------------------+-----------+

related_fields , .. related_text , , related_id , , . , status_id active_service related_id related_fields, , .. related_text ActiveServices_status. . , ( ActiveServices).

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(

        'rl_status'=>array(self::BELONGS_TO,'RelatedFields','status_id','condition'=>'related_text = "ActiveServices_status"','on'=>'status_id = related_id'),
    );
}

.

+4
3

, - , 100 . , .

'rl_status' => array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('status_id'=>'related_id'),'condition'=>'related_text = "ActiveServices_status"',
+12

Yii 1.1.9 ; . # 2706. , , , .

, , "fk1" "fk2", "col1" "col2" "Foo", :

'foo' => array(self::BELONGS_TO, 'Foo', array('fk1'=>'col1','fk2'=>'col2'))

CActiveRecord.relations():

PK- > FK, ('fk' = > 'pk').

+1
'rl_status' => array(self::BELONGS_TO, 'RelatedFields', '', 'foreignKey' => array('status_id'=>'related_id'),'condition'=>'related_text = "ActiveServices_status"'
+1
source

All Articles