Help getting related fields from CActiveDataProvider

Hello.

I can’t get related fields from a CActiveDataProvider.

First, here’s the relation for the model Categoria:




'categoriaIdiomas' => array(self::HAS_MANY, 'CategoriaIdioma', 'categoria_id', 'condition'=>"categoriaIdiomas.idioma_id='1'"),



this is the CActiveDataProvider call:




$dataProvider=new CActiveDataProvider(Categoria::model()->with('categoriaIdiomas')->together()->activas());



activas() is a scope:




'activas'=>array(

	'condition'=>'categoria_estatus=1',

	'order'=>'categoria_orden asc',

),



When I execute this, the log says the sql queries are:




SHOW COLUMNS FROM `categoria`

SHOW CREATE TABLE `categoria`

SHOW COLUMNS FROM `categoria_idioma`

SHOW CREATE TABLE `categoria_idioma`

SELECT COUNT(DISTINCT `t`.`id`) FROM `categoria` `t` LEFT OUTER JOIN `categoria_idioma` `categoriaIdiomas` ON (`categoriaIdiomas`.`categoria_id`=`t`.`id`) WHERE (categoria_estatus=1) AND (categoriaIdiomas.idioma_id='1')

SELECT `t`.`id` AS `t0_c0`, `t`.`categoria_estatus` AS `t0_c1`, `t`.`categoria_orden` AS `t0_c2`, `categoriaIdiomas`.`id` AS `t1_c0`, `categoriaIdiomas`.`categoria_id` AS `t1_c1`, `categoriaIdiomas`.`idioma_id` AS `t1_c2`, `categoriaIdiomas`.`categoria_nombre` AS `t1_c3` FROM `categoria` `t` LEFT OUTER JOIN `categoria_idioma` `categoriaIdiomas` ON (`categoriaIdiomas`.`categoria_id`=`t`.`id`) WHERE (categoria_estatus=1) AND (categoriaIdiomas.idioma_id='1') ORDER BY categoria_orden asc LIMIT 10



If I copy and execute the last sql query, it shows me that the columns are selected correctly (it includes the related value I am interested in).

When I try to call it on my view I get errors, depending on how I try to access the related value.

If I try:




<?php echo CHtml::encode($data->categoriaIdiomas->categoria_nombre); ?>



I get:




Trying to get property of non-object



If I try:




<?php echo CHtml::encode($data->categoria_nombre); ?>



I get:




Property "Categoria.categoria_nombre" is not defined.



And finally, if I do a print_r($data), I can see that the value is there!




[_new:CActiveRecord:private] => 

                            [_attributes:CActiveRecord:private] => Array

                                (

                                    [id] => 3

                                    [categoria_id] => 3

                                    [idioma_id] => 1

                                    [categoria_nombre] => Objetos   // <-- there you are!

                                )



So, how do I get the value of categoriaIdiomas.categoria_nombre?

Thanks.

Since you have HAS_MANY relation, $data->categoriaIdiomas must return an array of related objects, so maybe you want $data->categoriaIdiomas[0]->categoria_nombre. Though you can’t be sure an array is not empty.

Thank you, thank you, thank you! :D

That works.

I understand the situation about not being sure if the array has a value or not.

Maybe I should try another approach, any ideas?

Do you need only 1 array value or maybe you want to merge all values with some separator, e.g. comma? :)

I am interested in only one value. Since it is the name for Categoria, there is only one value when I know what language the user has.

andy_s, Thank you :)