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.