我有一些具有外键引用的表:
User
-> Tech
-> TechSchedule
-> Location
->Customer
似乎我可以使用以下查询一次将任何相关数据提供给用户。考虑以下查询:
// load the user model
$model = User::model()->findByPk( Yii::app()->user->id );
// print
echo "<pre>", print_r( $model->attributes ), "</pre>";
// print more about the user
echo "<pre>", print_r( $model->Tech->TechSchedule[0]->Location->Customer ), "</pre>";
打印出来
Array
(
[user_id] => 1
[username] => someusername
[password] => somepassword
[salt] => somesalt
)
Customer Object
(
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[customer_id] => 14
[more customer data...]
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 14
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)`
这是正常行为吗?如果是这样,经历编写关系查询的麻烦的目的是什么,例如
$model = User::model()->with('Tech.TechSchedule.Location.Customer')->findByPk( Yii::app()->user->id );