In GRID access the values of the other reference tables

In grid how can I access the other table’s value.




        <?php

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

                        'id'=>'activity-role-map-grid',

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

                        'filter'=>$model,

                        'columns'=>array(

                                'id',

                                'activity_id', # want to access the text, such as activities.activity

                                'role_id',

                                array(

                                        'class'=>'CButtonColumn',

                                ),

                        ),

                ));

        ?>



In my interface there are three tables :

    1) activities


            - id integer


            - activity text





    2) roles


            - id integer


            - role text





    3) activity_role_map


            - id integer


            - activity_id integer references activities (id)


            - role_id integer references roles (id)

Relations created in the model files :

activities.php




    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(

            'id'=>array(self::HAS_MANY, 'activity_role_map', 'activity_id'),

        );

    }



roles.php




    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(

            'id'=>array(self::HAS_MANY, 'activity_role_map', 'role_id'),

        );

    }



activity_role_map.php




    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(

            'activity_id'=>array(self::BELONGS_TO, 'activities', 'activity'),

            'role_id'=>array(self::BELONGS_TO, 'roles', 'id')

        );

    }



Error Thrown,

Property "activity_role_map.activities" is not defined, here it takes the activities as the column name.

    How will I be able to list the name of the activity instead of id.

Untested, but if at all possible, the column spec syntax should have been be "id.activity_id.activity". But you already have a id column in the primary table so you should give the relationship you declared in the roles model a better name.

BTW It seems like you have a common MANY_MANY case here (unless you’re planning to add more columns to activity_role_map).

Perhaps my recent post in this thread can give a hint. (disregard the listview related comment about iteration).

Edit:

By using the detailed column spec syntax you should be able to use ‘value’=>’$data->id->activity_id[what_goes_here?]->activity’, but I still see a problem with the array indexing.

Edit2: I agree with user bleed below (using activity_role_map as primary makes it simple). One really shouldn’t care to answer questions here unless able to spend an appropriate amount of time on considering possible options. :rolleyes:

/Tommy

your relations are wrong




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(

            'activity'=>array(self::BELONGS_TO, 'activities', 'activity_id'),

            'role'=>array(self::BELONGS_TO, 'roles', 'role_id')

        );

    }

fnd in your view file smth like this


<?php

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

                        'id'=>'activity-role-map-grid',

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

                        'filter'=>$model,

                        'columns'=>array(

                                'id',

                                'activity.activity', 

                                'role.rile',

                                array(

                                        'class'=>'CButtonColumn',

                                ),

                        ),

                ));

        ?>

Hi I have the same problem discussed here, i tried your solution but i have a "not set" displayed.

I have two models(tables) : Equipement and LibelleEquipement.

In Equipement i define the following relations function :




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(

                        'libelle' => array(self::BELONGS_TO, 'LibelleEquipement', 'libele'),

		);

	}



And in the LibelleEquipement I define the following relations function :




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(

			'equipements' => array(self::HAS_MANY, 'Equipement', 'libele')

		);

	}



In my view (details) in use CDetailView like this :




<?php $this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'libele.libelle',

		'annee',

		'quantite',

		'modele',

		'reference',

		'puissance',

		'produit',

		'type_enveloppe',

		'jauge',

		'peh_methalique',

		'fabriquant',

		'type_equipement',

		'numero_serie',

		'marque',

		'capacite',

		'id',

		'num',

		'tbl_station_id',

	),

)); ?>



Thanks for your help!