3 tabelas

17 views
Skip to first unread message

Rodrigo Maia

unread,
Oct 29, 2009, 1:55:08 PM10/29/09
to cake-...@googlegroups.com
Olá pessoal, estou fazendo o seguinte. Tenho 3 models (Noticia, MenuN1, MenuN2)
MenuN1 hasMany MenuN1
MenuN2 belongsTo MenuN1
MenuN2 hasMany Noticia
Noticia belongsTo MenuN2

Quando dou um $this->Noticia->find('all') ele me retorna as noticias e dados somente do MenuN2. Gostaria de trazer os dados do MenuN1 também já que o esquema é estilo Categoria, Subcategoria mas as notícias se relacionam com Subcategoria e já que uma subcategoria faz parte de uma categoria está notícia tambem faz parte desta categoria.

Como fazer para na consulta $this->Noticia->find('all') trazer dados destes relacionamentos e não somente de MenuN2?

Obrigado.

--
Rodrigo Maia
http://depoisdas10.com
rod...@gmail.com

Vinícius Krolow

unread,
Oct 29, 2009, 2:21:03 PM10/29/09
to cake-...@googlegroups.com
var $actAs = array('Containable'); no modelo das notícias

$this->Noticia->contain('MenuN2', 'MenuN2.MenuN1', 'MenuN2.MenuN1.MenuN1');

e o findall

com o behavior containable tu consegue dizer o que trazer dos relacionamentos de modelos.

2009/10/29 Rodrigo Maia <rod...@gmail.com>



--
Vinícius Krolow
--------------------------------------------------------------------------
Developer

blog: http://cobaia.net
irc: krolow
msn: vini...@cobaia.net

Vinícius Krolow

unread,
Oct 29, 2009, 2:21:23 PM10/29/09
to cake-...@googlegroups.com
Pode te algo errado mas acho que por ae tu consegue chegar no que tu quer.

2009/10/29 Vinícius Krolow <kro...@gmail.com>

Rodrigo Maia

unread,
Dec 3, 2009, 6:02:36 PM12/3/09
to cake-...@googlegroups.com
Olá pessoal, retornando ao assunto. Estou com dificuldades de conseguir faze funcionar isso.
Tenho as seguintes tabelas:

Tabela (menu_n1)

  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `nome` VARCHAR(100) NOT NULL,
  `slug` VARCHAR(100) NOT NULL,
  `destaque` ENUM('S','N')  NOT NULL,
  `meta_descricao` VARCHAR(255) NOT NULL,
  `meta_palavras` TEXT  NOT NULL,
  `ativo` ENUM('S','N')  NOT NULL,
  `posicao_n1` INT(11) NOT NULL,
  `title` VARCHAR(255)
  PRIMARY KEY (`id`),

Tebela (menu_n2)


  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `menu_n1_id` INT(11) NOT NULL,
  `nome` VARCHAR(100) NOT NULL,
  `slug` VARCHAR(100) NOT NULL,
  `destaque` ENUM('S','N') NOT NULL,
  `meta_descricao` VARCHAR(255) NOT NULL,
  `meta_palavras` TEXT NOT NULL,
  `ativo` ENUM('S','N') NOT NULL,
  `posicao_n2` INT(11) NOT NULL,
  `title` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`),

Tabela (noticias)

  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `layout` INT(11) DEFAULT NULL,
  `menu_n2_id` INT(11) DEFAULT NULL,
  `titulo` VARCHAR(100) DEFAULT NULL,
  `subtitulo` VARCHAR(250) DEFAULT NULL,
  `conteudo` LONGTEXT,
  `autor` VARCHAR(100) DEFAULT NULL,
  `imagem` VARCHAR(100) DEFAULT NULL,
  `data_hora` DATETIME DEFAULT NULL,
  `status_noticia` ENUM('S','N') DEFAULT NULL,
  `views` BIGINT(20) DEFAULT NULL,
  PRIMARY KEY (`id`),

Model MenuN1

var $useTable = 'menu_n1';
var $hasMany = 'MenuN2';

Model MenuN2

var $useTable  = 'menu_n2';
var $belongsTo = 'MenuN1';
var $hasMany   = 'Noticias';

Model Noticia

var $belongsTo = array('MenuN2');

var $actAs = array('Containable');

No caso fiz como Vinícius falou mas não vem a slug do menu_n1 de jeito nenhum.


$this->Noticia->contain('MenuN2', 'MenuN2.MenuN1', 'MenuN2.MenuN1.MenuN1');

Vasculhei os exemplos em http://book.cakephp.org/pt/view/474/Containable mas não ví nada relacionando 3 tabelas.

Já até pensei em fazer uma query mas deve existir a maneira certa de se fazer isso.

Onde estou errando? Alguém tem exemplos ou passou por alguma necessida igual a minha e pode me ajudar?
Obrigado.


2009/10/29 Vinícius Krolow <kro...@gmail.com>
Reply all
Reply to author
Forward
0 new messages