Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Categorias Com Parent_Id

$
0
0
Estou tentando montar uma tabela de 'Categorias' com a seguinte estrutura:


CREATE TABLE IF NOT EXISTS `categoria` (
`IDCategoria` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(255) NOT NULL,
`descricao` text NOT NULL,
`parent_id` int(11) NOT NULL,
`top` int(11) NOT NULL,
`ordem` int(11) DEFAULT '0',
`status` int(11) NOT NULL,
PRIMARY KEY (`IDCategoria`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

- na model:
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(
'categoria_parent' => array(self::BELONGS_TO, 'Categoria', 'parent_id', 'condition' => 'parent_id <> 0'), //the column in 'parent' containing 'this' id
'categoria_childs' => array(self::HAS_MANY, 'Categoria', 'parent_id'),
);
}


- Na minha view ADMIN da model 'Categorias' tenho isso:

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'categoria-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'IDCategoria',
'parent_id',
'status',
'nome',
array(
'name'=>'nome',
//'header'=>'Setor',
'value'=>'$data->parent_id > 0 ? $data->getChilds($data->parent_id,$data->nome) : $data->nome',
'htmlOptions'=>array('style'=>'text-align:center;'),
),
'ordem',

array(
'class'=>'CButtonColumn',
),
),
)); ?>


- E grande questão é o seguinte...Queria listar na minha Grid as categorias dessa forma:


Panelas > Caldeirões
Panelas > Caçarolas
Panelas > Caçarolas > Alumínio
Panelas > Pressão
Utensílios
Mesas

De forma que eu pudesse passar o $parent_id e ele me retornasse os elementos:
Algo como $data->getChilds($data->parent_id,$data->nome);

Alguma ideia?
Ja pesquisei tem muitos lugares, mas nenhum na mesma situação que eu.

HEEEEEEEEEEEELP...

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>