Hi all,
I’m newbie to Yii. I need manging a simple table (“machine”) on db server1, and have to view/update another table (“user”) on db server2, by joining the field “support”. The tables are created as following:
On database server1, database1:
create table machine (
id integer not null primary key auto_increment,
name varchar(128) not null,
support integer not null
);
On database server2, database1:
create table user (
id integer not null primary key auto_increment,
staff varchar(128) not null,
email varchar(128) not null,
support integer not null
);
i.e. the view contains name, staff, email, and support fields.
I tried following:
-
create 2 models, one for machine (Machine.php) and one of user (User.php), using different db arrays in config/main.php
-
define relationship in models/Machine.php:
public function relations() { return array( 'UserName' => array(self::BELONGS_TO, 'User', 'support'), );
-
modify controllers/MachineController.php:
// $model=Machine::model()->findByPk($id);
$model=Machine::model()->with("UserName")->findAll();
- modify views/machine/view.php to display the values:
<?php $this->widget(‘zii.widgets.CDetailView’, array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
'support',
// added following 2 arrays
array(
'name'=>'staff',
'value'=>'$data->UserName->staff'
),
array(
'name'=>'email',
'value'=>'$data->UserName->email'
)
),
)); ?>
Only attributes in the machine table are displayed as if no relationship is defined. Would anyone please help? Sorry for the newbie questions.
Thanks a lot.
Regards,
uReg