Please Help: Relational Active Record Usage

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'=&gt;&#036;model,


    'attributes'=&gt;array(


            'id',


            'name',


            'support',

// added following 2 arrays

            array(


                    'name'=&gt;'staff',


                    'value'=&gt;'&#036;data-&gt;UserName-&gt;staff'


            ),


            array(


                    'name'=&gt;'email',


                    'value'=&gt;'&#036;data-&gt;UserName-&gt;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