Join Two Tables In Crud Operations?

I’m new to yii… I have problem related to CRUD operation…

in my case, I have two tables as follows,

table 1 - hospital

CREATE TABLE my_database.hospital (

hospitalId INT NOT NULL ,

hospital_name VARCHAR(145) NOT NULL ,

address TEXT NOT NULL ,

city VARCHAR(45) NOT NULL ,

PRIMARY KEY (hospitalId) );

table 2 - patient

CREATE TABLE my_database.patient (

patientId INT NOT NULL AUTO_INCREMENT ,

name VARCHAR(100) NOT NULL ,

nic VARCHAR(12) NULL ,

hospitalId INT(15),

PRIMARY KEY (patientId) );

So I have create CRUD operation for patient table.Both tables connected with hospitalId. I want to display hospital_name in view file instead of hospitalId. Now it automatically displayed hospitalId . How it could be possible? can any one help me? Thank you…

You need to setup a relation to that table, and you can use the relation to reference that field like this:


$data->member->name;

In your transaction model you would place something like:




public function relations() {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'member' => array(self::BELONGS_TO, 'Member', 'member_id'),// foreign key

        );

    }



and for grid you would do like:


$this->widget('zii.widgets.grid.CGridView', array(

'id'=>'transaction-grid',

'dataProvider'=>$model->search(),

'filter'=>$model,

'columns'=>array(

    'id',

       array(

            'header' => 'Member',

            'name' => 'member_id',

            'value' => '$data->member->name'

        ),


    'Location',

    ...

    array(

        'class'=>'CButtonColumn',

        'template'=>'{view}',

    ),

),

));

I hope it’some help.

Очень забавная штука

Thanks Maggie Q … it is working… it would be great… (Y)