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()
{
return array(
'rl_status'=>array(self::BELONGS_TO,'RelatedFields','status_id','condition'=>'related_text = "ActiveServices_status"','on'=>'status_id = related_id'),
);
}
.