October 10, 2012, 7:33pm
I have 5 related tables.
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
October 12, 2012, 10:17am
Try something like this:
'width'=>array("relationNameToTbl4" => array(
'condition' => "your condition",
Can you please describe your relevant table foreign key columns in detail, thanks.
October 13, 2012, 9:22am
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()
'Tbl3_to_Tbl1_relation' => array(
'alias' => 'Tabel1',
'condition' => 'Tabel1.PK1 = :myID',
'params' => array(':myID' => Yii::app()->user->userID),
'Tbl3_to_Tbl2_relation' => array(self::BELONGS_TO, 'Tabel2', 'PK2'),
/* 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.