licass
(L R M D)
June 11, 2014, 9:22am
1
Hi, how can i get the name of product in many many relation.
ex:
produto model
'tabuleiro' => array(self::MANY_MANY, 'tabuleiro', 'tabuleiro_produto(id_produto,id_tabuleiro)'),
tabuleiro model
'produto' => array(self::MANY_MANY, 'produto', 'tabuleiro_produto(id_produto,id_tabuleiro)'),
then i try to use ĩn cgridview
'tabuleiro_id' => array(
'name' => 'tabuleiro_id',
'type' => 'raw',
'value' => '$data->tabuleiro->descricao',
'value' => 'Chtml::link(Chtml::encode($data["tabuleiro_id"]),array("tabuleiro/view","id"=>$data["tabuleiro_id"]))',
'filter' => CHtml::listData(Tabuleiro::model()->findAll(array('order' => 'descricao')), 'id', 'descricao')
),
but cant get the descricao field from Tabuleiro model.
Keith
(Kburton)
June 11, 2014, 10:21am
2
If it’s a many-many relationship then the tabuleiro relation will return an array of models. Do you want to display the descriptions of all of them or just the first?
licass
(L R M D)
June 11, 2014, 1:18pm
3
Just the first.
but by the way , how can i display all.
thanks keith
Keith
(Kburton)
June 11, 2014, 1:31pm
4
Something like this:
'value' => 'isset($data->tabuleiro[0]) ? $data->tabuleiro[0]->descricao : ""',
To show all, you’d either iterate through each element in $data->tabuleiro or use a combination of implode() and array_map().
licass
(L R M D)
June 11, 2014, 1:40pm
5
Keith:
Something like this:
'value' => 'isset($data->tabuleiro[0]) ? $data->tabuleiro[0]->descricao : ""',
To show all, you’d either iterate through each element in $data->tabuleiro or use a combination of implode() and array_map().
now have a problem with relation.
in produto model
'precos' => array(self::HAS_MANY, 'Preco', "id_produto"),
precos model
'produto' => array(self::BELONGS_TO, 'produto', 'id_produto'),
i have records in precos but nothing is returned.
can you help me?
licass
(L R M D)
June 11, 2014, 1:42pm
7
"precos" => array(
'name' => 'precos',
'type' => 'raw',
'value' => array($this, 'gridGroupedPrice')
),
protected function gridGroupedPrice($data, $row) {
if ($this->_precos != $data->precos) {
$this->_precos = $data->precos;
return $data->precos;
} else
return 'nada';
}