VIEWS

am new to yii.I am developing a web application where ive used foreign keys. i would like to include other columns of a parent table in my views rather than just the foreign key column and other columns of the child table . help

Table Parent:

-id_P

-first_name

-last_name

-age

Table Child:

-id_C

-first_name

-last_name

-age

-id_P

In the Child model:




class Child extends CActiveRecord {


public $parent_first_name;

public $parent_last_name;

...

...

	public function rules()

	{

		return array(

			...

                        ...

			array('...,...,parent_first_name, parent_last_name,...', 'safe', 'on'=>'search'),

		);

	}

...

...

    function relations() {

        return array(

            'parent'=>array( self::BELONGS_TO, 'Parent', 'id_P' ),

        );

    }

...

}



In the Child’s admin.php (view):




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

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

    'filter'=>$model,

    'columns'=>array(

        'id_C',

        'first_name',

        'last_name',

        'age',

        array(

           'name'=>'parent_first_name',

           'value'=>'$data->parent->first_name'

        ),

        array(

           'name'=>'parent_last_name',

           'value'=>'$data->parent->last_name'

        ),

    ),

));



hello thank you. though im getting a php warning :

include(Buildings_BUILDING_NAME.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

in my case Buildings is the parent table its primary key is BUILDING_ID . with BUILDING_NAME as the other column6375

admin.php

my child table is maintenance request using the BUILDING_ID as foreign key

6376

Maintenancerequest.php

You have some misspelling in relation name, try the following




'building' => array(self::BELONGS_TO, 'Buildings', 'BUILDINGS_BUILDING_ID')






'value'=>'$data->building->BUILDING_NAME'



Also make sure the name of the Model is written correctly as second attribute in relation array (eg. if you have model BuildingsXYZ.php then:




'building' => array(self::BELONGS_TO, 'BuildingsXYZ', 'BUILDINGS_BUILDING_ID')



)

and the third attribute in the relation is the fk used in the table relation, so you have to write it’s name exactly like it is in database

Thank you so much, sorry i forgot to say this

Also assist me on how i would include search on the parentname column in admin view

I suggest that you look at the source code of the demo blog that comes with Yii.