cakephp - Can't find model associated model data -
i'm new in cakephp , i'm having problems in example i'm building learn.
i'm building plugin 3 models (categoria,plato,imagen)
the relationships between them following: categoria - plato (n-n) plato - imagen (1-n)
if go plato view, imagen relationship, when access through category, can't reach imagen associated each plato. why? what's problem?
models code:
plato:
app::uses('appmodel', 'model'); class plato extends erestaurantappmodel { public $name = 'plato'; //public $actsas = array('containable'); public $hasandbelongstomany = array( 'categoria' => array( 'classname' => 'categoria', 'jointable' => 'categorias_platos', 'foreignkey' => 'plato_id', 'associationforeignkey' => 'categoria_id', 'unique' => 'keepexisting', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderquery' => '', ) ); public $hasmany = array('imagen' => array('foreingkey' => array('plato_id'))); }
categoria:
app::uses('appmodel', 'model'); class categoria extends erestaurantappmodel { public $name = 'categoria'; //public $actsas = array('containable'); public $hasandbelongstomany = array( 'plato' => array( 'classname' => 'plato', 'jointable' => 'categorias_platos', 'foreignkey' => 'categoria_id', 'associationforeignkey' => 'plato_id', 'unique' => 'keepexisting', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'finderquery' => '', ) ); }
imagen:
app::uses('appmodel', 'model'); class imagen extends erestaurantappmodel { public $name = 'imagen'; //public $actsas = array('containable'); public $belongsto = array('plato'); }
finally, code i'm launching when go categoria view.
app::uses('appcontroller', 'controller'); class categoriascontroller extends erestaurantappcontroller { public $uses = array('erestaurant.categoria'); public function index() { $this->categoria->recursive = 0; $this->set('data', $this->categoria->find('all')); } public function view($id = null) { if (!$this->categoria->exists($id)) { throw new notfoundexception(__('registro inválido.')); } /* $this->categoria->recursive = -1; $options = array('conditions' => array('categoria.'.$this->categoria->primarykey => $id), 'contain' => array( 'plato' => array( 'imagen' ) ) ); */ $this->categoria->recursive = 2; $options = array('conditions' => array('categoria.'.$this->categoria->primarykey => $id)); $this->set('item', $this->categoria->find('first', $options)); } }
the commented code see it's way tried, using containable, @ end error "plato not associated model imagen"
database seems ok, each table created needed (categorias, platos, imagens, categorias_platos) , foering keys created , trying keep cakephp default names.
thank you!
to access deep relations must use containable behavior. have definition of containable behavior in model, reason commented.
public $actsas = array('containable');
uppon uncommenting line in categoria, can access deep relations in way:
$this->categoria->find('all', array( 'contain'=>array( 'plato.imagen' ) ));
...or this. more information plase visit link containable behavior
Comments
Post a Comment