cakephp - Association between two models not working -
cakephp - Association between two models not working -
using cakephp 3.0, have (amongst others) 2 models associated:
<?php # src/model/table/informationtable.php namespace app\model\table; class informationtable extends apptable { public function initialize(array $config) { parent::initialize($config); $this->belongsto('contractor'); $this->belongsto('informationfield'); } } ?>
and
<?php # src/model/table/informationfieldstable.php namespace app\model\table; class informationfieldstable extends apptable { public function initialize(array $config) { parent::initialize($config); $this->hasmany('information'); } } ?>
in contractors
controller, related models, next works fine:
$this->contractors->information
but when seek access informationfields
model, error:
$this->contractors->information->informationfields
table "app\model\table\informationtable" not associated "informationfields"
i did little testing inflector
class , singularization , pluralization of "information"<->"information" , "informationfield"<->"informationfields" works expected.
also, when placing debug()
phone call in both models initialize()
methods , fetching models through tableregistry
, both initialize()
methods called. in above scenario $this->contractors->information->informationfields
, initialize()
method of informationtable
class run.
i stuck @ point. why association not working correctly?
edit: here more test cases:
$informationtable = tableregistry::get('information'); debug($informationtable->informationfields); #error: table "app\model\table\informationtable" not associated "informationfields" $informationfieldstable = tableregistry::get('informationfields'); debug($informationfieldstable->information); #no error
you wrote $this->belongsto('informationfield');
when should have been
$this->belongsto('informationfields');
(with s)
cakephp model-associations cakephp-3.0
Comments
Post a Comment