php - Cakephp 3 belongsToMany results in undefined offset/index -


i have "groups" , "servers" tables need join through many many relationship. created third table in database "mn_groups_servers" columns id, group_id , server_id. added groupstable:

    public function initialize(array $config) {     parent::initialize($config);     $this->table('mn_groups');      $this->belongstomany('servers', [         'jointable' => 'mn_groups_servers',         'foreignkey' => 'group_id',         'targetforeignkey' => 'server_id',     ]);  } 

and serverstable:

    public function initialize(array $config) {     parent::initialize($config);             $this->table('mn_servers');      $this->belongstomany('groups', [         'jointable' => 'mn_groups_servers',         'foreignkey' => 'server_id',         'targetforeignkey' => 'group_id',     ]); } 

then in groupscontroller try fetch results with:

    public function index() {     $groups = $this->groups->find('all')->contain('servers');     $this->set(compact('groups')); } 

this results in 2 errors:

notice (8): undefined offset: 0 [core\src\orm\association\selectableassociationtrait.php, line 286]  cake\orm\association\belongstomany::_resultinjector() - core\src\orm\association\selectableassociationtrait.php, line 286 cake\orm\association\belongstomany::eagerloader() - core\src\orm\association\selectableassociationtrait.php, line 57 cake\orm\eagerloader::loadexternal() - core\src\orm\eagerloader.php, line 542 cake\orm\query::_execute() - core\src\orm\query.php, line 657 cake\orm\query::_all() - core\src\datasource\querytrait.php, line 218 cake\orm\query::all() - core\src\orm\query.php, line 608 cake\orm\query::getiterator() - core\src\datasource\querytrait.php, line 132 include - app/template\groups\index.ctp, line 10 cake\view\view::_evaluate() - core\src\view\view.php, line 834 cake\view\view::_render() - core\src\view\view.php, line 794 cake\view\view::render() - core\src\view\view.php, line 465 cake\controller\controller::render() - core\src\controller\controller.php, line 582 cake\routing\dispatcher::_invoke() - core\src\routing\dispatcher.php, line 120 cake\routing\dispatcher::dispatch() - core\src\routing\dispatcher.php, line 87 require - root\webroot\index.php, line 37 [main] - root\index.php, line 16 

and

notice (8): undefined index:  [core\src\orm\association\selectableassociationtrait.php, line 288]  cake\orm\association\belongstomany::cake\orm\association\{closure}() - core\src\orm\association\selectableassociationtrait.php, line 288 cake\database\statement\callbackstatement::fetch() - core\src\database\statement\callbackstatement.php, line 58 cake\orm\resultset::_fetchresult() - core\src\orm\resultset.php, line 477 cake\orm\resultset::valid() - core\src\orm\resultset.php, line 269 include - app/template\groups\index.ctp, line 10 cake\view\view::_evaluate() - core\src\view\view.php, line 834 cake\view\view::_render() - core\src\view\view.php, line 794 cake\view\view::render() - core\src\view\view.php, line 465 cake\controller\controller::render() - core\src\controller\controller.php, line 582 cake\routing\dispatcher::_invoke() - core\src\routing\dispatcher.php, line 120 cake\routing\dispatcher::dispatch() - core\src\routing\dispatcher.php, line 87 require - root\webroot\index.php, line 37 [main] - root\index.php, line 16 

cake still fetches groups table objects, no results servers table.

note have prefix mn tables database organisation. tried associations without prefixes same results. i'm using sql server database. cake version 3.0.10.

what cause this?

judging errors , code, looks database tables have no primary key constraints set, necessary cakephp find column(s) should act primary key, unless explicitly specify column(s) should used.

add appropriate constraints id columns , should work (don't forget clear model cache afterwards src/tmp/cache/models).

explicitly telling table class column(s) use possible using table::primarykey() method.

// in tables initialize() method $this->primarykey('id'); 

however, should done additionally (this avoid processing schema), , not alternatively! tables should have pk constraints set unless have very reason not use them!

see also


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -