display values of many many relationships in gridview

Hi all,

I would like to show the values of my many many relationship in a gridview in the admin view of Mueble model. My problem is that i can not get the values of the colors.

these are my models:




      +-------+      +------------------+     +--------+

      |Mueble |      |acabadocolormueble|     |color   |

      +-------+      +------------------+     +--------+

      |id     |-----<|mueble_id         |  +--|id      |

      |name   |      |color_id          |>-+  |name    |

      +-------+      +------------------+     +--------+



My model Mueble:





        // Mueble model


	public function getRelatedColorEstructuraNames()

	{

		$out=CHtml::listData($this->coloresestructura,'id','name');


		return implode(',<br />', $out);

	}


	public function relations()

	{

	        return array(

		     'coloresestructura' => array(self::MANY_MANY, 'ColorMueble', 

                                            'acabadocolormueble(fkcolormueble, fkmueble, fktipoacabado)',

                                            'condition' => 'fktipoacabado = 1'),

	}



My admin view:




        // Mueble admin view

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

	'id'=>'mueble-grid',

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

    ................

        array(

		   'name'=>'coloresestructura',

		   'type'=>'html',

		   'value'=>'$data->getRelatedColorEstructuraNames()',

		),

	.............



should i have something else? there is no a lot of information about many many relationships and yii…

thanks in advance,

Hi hibernator,

Your definition of ‘coloresestructura’ relation looks strange …

It should be something like:




	public function relations()

	{

	        return array(

		     'coloresestructura' => array(self::MANY_MANY, 'ColorMueble', 

                                            'acabadocolormueble(mueble_id, color_id)',

                                            'condition' => '... some condition ...'),

	}



According to the reference:

  1. "For foreign keys used in MANY_MANY relation, the joining table must be declared as well (e.g. ‘join_table(fk1, fk2)’). "

But yours looks ‘join_table(fk1,fk2,fk3)’.

  1. And for the ‘condition’ option, “column references need to be disambiguated with prefix ‘relationName.’ (e.g. relationName.age>20)”

What’s ‘fktipoacabado’? Which table does it belong to?

http://www.yiiframework.com/doc/api/1.1/CActiveRecord/#relations-detail

also, your say model (class name) is "color" not "ColorMueble"…

Thanks!

after two days searching on google i found the solution:

‘coloresestructura’ => array(self::MANY_MANY, ‘ColorMueble’, ‘acabadocolormueble(fkmueble, fkcolormueble)’,),

the order of the foreign keys!

now i am going to try the condition.

tipoacabadomueble is an attribute for the many many relationship like an enum, now tipoacabadomueble is a model but i saw that i can define the types as constants…

thanks in advance,

Um, I don’t understand. Could you elaborate it a little more?

Hi,

Just in case anybody had the same problem. As you can see in my relations model, i have two many many relationships, each one with a type. The meaning of this is that i have some front colors and some structure colors and i want to know which type has each color…

acabadocolormueble has three columns (fkmueble, fkcolormueble,fktipoacabado).




//relations Mueble model

'coloresestructura' => array(self::MANY_MANY, 'ColorMueble', 'acabadocolormueble(fkmueble, fkcolormueble)', 'condition' => 'fktipoacabado = 1'),

'coloresfrontal' => array(self::MANY_MANY, 'ColorMueble', 'acabadocolormueble(fkmueble, fkcolormueble)', 'condition' => 'fktipoacabado = 2'),



thanks in advance,

I see.

Does it work?

yes,

it works :)

hibernator,

Thank you hibernator,

I thought that it doesn’t work, because “fktipoacabado” is not disambiguated by the table alias.

But I was wrong.

When there is no other column with the same name in the joined tables, then we don’t need to disambiguate it. And as long as “coloresestructura” and “coloresfrontal” are loaded lazily, there’s no collision between the two "fktipoacabado"s.

OK. I think it’s clear now.

Thank you again. :)

Hi hibernator,

I have a problem similar to this topic, which u had.

can i know the primary key or indexes used in ‘acabadocolormueble’ table?