All of the respective model associations have been configured correctly on my end.
Here's my database SQL script so you can easily see the foreign key associations:
create table proveedors
(
id int primary key AUTO_INCREMENT,
nombre varchar(256)
);
create table libros
(
id int primary key AUTO_INCREMENT,
proveedor_id int,
FOREIGN KEY (proveedor_id) REFERENCES proveedors(id),
titulo varchar(1024),
autor varchar(512)
);
create table materias
(
id int primary key AUTO_INCREMENT,
nombre varchar(1024),
version varchar(8)
);
create table libros_materias
(
id int primary key AUTO_INCREMENT,
materia_id int,
FOREIGN KEY (materia_id) REFERENCES materias(id),
libro_id int,
FOREIGN KEY (libro_id) REFERENCES libros(id)
);
Here's a query I'm running:
# Buscar todos los libros asociados a esta materia.
$libros = $this->LibrosMateria->findAllByMateriaId($id);
$this->set('libros', $libros);
Right now it's fetching all data correctly as expected; I just need to make the query do one more little hop and get me the Proveedor's name
.
The current query is returning this:
(int) 0 => array(
'LibrosMateria' => array(
'id' => '2',
'materia_id' => '1',
'libro_id' => '1'
),
'Materia' => array(
'id' => '1',
'tipo_materia_id' => '1',
'sigla' => 'MKT-111',
'nombre' => 'Direccion de Marketing',
'version' => '3-2012'
),
'Libro' => array(
'id' => '1',
'proveedor_id' => '1',
'titulo' => 'PHP 2.0',
'autor' => 'Arhari',
'edicion' => '3ra Edicion',
'isbn' => '0912309',
'cantidad_de_paginas' => '912',
'fecha_de_publicacion' => '2012-10-08',
'portada' => null
)
),
I can see it's making 1 single foreign key hop, but not going further. Is there some way for CakePHP to do this? Any suggestions? Remeber: My model associations are set up correctly.
Try changing your query from:
$libros = $this->LibrosMateria->findAllByMateriaId($id);
to:
$libros = $this->LibrosMateria->findAllByMateriaId($id, array(), array(), null, null, 2);
Here you are setting the $recursive field to be 2, which should retrieve the information you want.
For more information see http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#findallby