gerhard
(gerhard@ecolar.co.za)
October 10, 2012, 7:33pm
1
Hi guys,
I have 5 related tables.
Table1
PK1
Table2
PK2
(PK1+PK3)
Table3
PK3
Table4
PK4
(PK3+PK5)
Table5
PK5
Table1 has a defaultScope of:
'condition' => "t.PK1=:parameterPK",
'params' => array(
': parameterPK ' => Yii::app()->user->userID,
),
By using defaultScope, I want all tables to only show records related to table1’s PK.
So if Table1.PK1 = 12, then all tables must only show records related to Table1.PK1 = 12.
And if I change Yii::app()->user->userID, then all tables must now only show records related to the new userID.
What must Table5’s defaultScope look like to do this?
Something like: "with table4 with table3 with table2 with table1". This should force Table5 to only show records related to the value of Table1.PK1
Any suggestions?
kokomo
(Mwerlberger85)
October 12, 2012, 10:17am
2
Try something like this:
return array(
'width'=>array("relationNameToTbl4" => array(
'condition' => "your condition",
)
);
Can you please describe your relevant table foreign key columns in detail, thanks.
Further help:
CDbCriteria.with
CActiveRecord.with
gerhard
(gerhard@ecolar.co.za)
October 13, 2012, 9:22am
3
Thanx Kokomo
I got it going from Tabel3 to Tabel1. I got a defaultScope in Table3 depending on the values of records in Table1.
To go from Tabel5 directly to Tabel1, I guess you simply have to add more relations in Tabel5 to link Tabel5 to this Tabel3.
class Tabel3 extends CActiveRecord
...
public function defaultScope()
{
return array(
'with'=>array(
'Tbl3_to_Tbl1_relation' => array(
'alias' => 'Tabel1',
'condition' => 'Tabel1.PK1 = :myID',
'params' => array(':myID' => Yii::app()->user->userID),
)
)
);
}
...
Relations:
'Tbl3_to_Tbl2_relation' => array(self::BELONGS_TO, 'Tabel2', 'PK2'),
'Tbl3_to_Tbl1_relation'=>array(self::HAS_ONE, 'Tabel1',
array('FK2'=>'PK1'),'through'=>'Tbl3_to_Tbl2_relation'),
/* PK1 = Table1 primary key. FK2 = Table2 foreign key of PK1 */
Note: I had to create a second Tbl3_to_Tbl2_relation for Cgridview sorting and filtering. The sql produced an error if only one Tbl3_to_Tbl2_relation existed in the model and it was used for both the defaultScope and for the gridview as well.