简体   繁体   English

Cakephp 3 EmiratesToMany导致未定义的偏移量/索引

[英]Cakephp 3 belongsToMany results in undefined offset/index

I have "Groups" and "Servers" tables that i need to join through many to many relationship. 我有“组”和“服务器”表,我需要通过多对多关系进行联接。 I created third table in database "mn_groups_servers" with columns id , group_id and server_id . 我在数据库“ mn_groups_servers”中创建了第三个表,其列为idgroup_idserver_id I added to GroupsTable: 我添加到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 to ServersTable: 并到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 i try to fetch the results with: 然后在GroupsController中,我尝试使用以下方法获取结果:

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

This results in two 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 the Groups table objects, but with no results from Servers table. Cake仍会获取“ Groups表对象,但“ Servers表没有任何结果。

Note that i have prefix mn for tables for database organisation. 请注意,我为数据库组织的表添加了前缀mn I tried the associations without the prefixes with the same results. 我尝试了不带前缀的关联,但结果相同。 Also i'm using SQL Server database. 我也正在使用SQL Server数据库。 My cake version is 3.0.10. 我的蛋糕版本是3.0.10。

What could be the cause for this? 这可能是什么原因?

Judging from the errors and the code, this looks like your database tables have no primary key constraints set, which is necessary for CakePHP to find the column(s) that should act as the primary key, unless you explicitly specify which column(s) should be used. 从错误和代码来看,您的数据库表似乎没有设置主键约束,这对于CakePHP查找应作为主键的列是必不可少的,除非您明确指定哪些列应该使用。

Add the appropriate constraints for your id columns and it should work (don't forget to clear the model cache afterwards src/tmp/cache/models ). 为您的id列添加适当的约束,它应该可以工作(不要忘了在src/tmp/cache/models之后清除模型缓存)。

Explicitly telling the table class which column(s) to use is possible using the Table::primaryKey() method. 使用Table::primaryKey()方法可以明确告诉表类要使用哪一列。

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

However, this should mostly really only be done additionally (this will avoid processing the schema), and not alternatively! 然而,这应该主要是真的只可以另外做(这将避免处理的模式),而不是交替! Your tables really should have the PK constraints set unless you have a very good reason to not use them! 你的表确实应该有PK的限制设置,除非你有一个很好的理由不使用它们!

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM