Cakephp 3 belongsToMany 结果未定义 offset/index

Cakephp 3 belongsToMany results in undefined offset/index

我有 "Groups" 和 "Servers" table 需要通过多对多关系加入。 我在数据库 "mn_groups_servers" 中创建了第三个 table,其中包含列 idgroup_idserver_id。 我添加到 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',
    ]);

}

然后到 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',
    ]);
}

然后在 GroupsController 中,我尝试使用以下方法获取结果:

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

这会导致两个错误:

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

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 仍然获取 Groups table 个对象,但是 Servers table.

没有结果

请注意,我为 table 的数据库组织设置了前缀 mn。 我尝试了没有前缀的关联,结果相同。 我也在使用 SQL 服务器数据库。 我的蛋糕版本是3.0.10.

这可能是什么原因?

从错误和代码来看,您的数据库 table 似乎没有设置主键约束,这是 CakePHP 找到应作为主键的列所必需的,除非您明确指定应使用哪一列。

为您的 id 列添加适当的约束,它应该可以工作(不要忘记在之后清除模型缓存 src/tmp/cache/models)。

可以使用 Table::primaryKey() 方法明确告诉 table class 要使用的列。

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

然而,这实际上应该只另外(这将避免处理架构),并且不是!您的 table 确实应该设置 PK 约束,除非您有 非常 不使用它们的充分理由!

另见