两个表之间的多个belongsToMany关联

时间:2019-05-20 20:28:57

标签: cakephp orm has-and-belongs-to-many cakephp-3.7

我想在两个表之间创建两个“虚拟”关系。

我有一个raw = '\x80\x57\x83\xa1\x64\xfa\xd4\x01' # Python 2 #raw = b'\x80\x57\x83\xa1\x64\xfa\xd4\x01' # Python 3 import struct unpacked, = struct.unpack('<Q', raw) import datetime datetime.datetime(1601,1,1) + datetime.timedelta(microseconds=unpacked/10.) # Outputs: # datetime.datetime(2019, 4, 24, 6, 12, 1, 400000) 表和一个Users表,并且我想将用户作为“作者”和/或“编辑者”与一个组相关联。

我有下表:

  • Groups
  • users: id | username
  • groups: id | name
  • groups_writers: id | user_id | group_id

我不确定如何在两个表之间创建多个关联。这是创建关系的尝试。

editors_groups: id | user_id | group_id

但这不会保存关联信息。

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('EditorGroups', [
            'joinTable' => 'editors_groups'
        ])
            ->setClassName('Groups')
            ->setForeignKey('user_id')
            ->setTargetForeignKey('group_id');

        $this->belongsToMany('WriterGroups', [
            'joinTable' => 'groups_writers'
        ])
            ->setClassName('Groups')
            ->setForeignKey('user_id')
            ->setTargetForeignKey('group_id');
    }
}

class GroupsTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('Editors', [
            'joinTable' => 'editors_groups'
        ])
            ->setClassName('Users')
            ->setForeignKey('group_id')
            ->setTargetForeignKey('user_id');

        $this->belongsToMany('Writers', [
            'joinTable' => 'groups_writers'
        ])
            ->setClassName('Users')
            ->setForeignKey('group_id')
            ->setTargetForeignKey('user_id');
    }
}

$group = $this->Groups->newEntity(); $data = [ 'name' => 'test group', 'editors' => [ '_ids' => [ (int) 0 => '1' ] ], 'writers' => [ '_ids' => [ (int) 0 => '2' ] ] ]; $group = $this->Groups->patchEntity($group, $data, [ 'associated' => ['Editors', 'Writers'] ]); $this->Groups->save($group); s

patchEntity

可能是因为object(App\Model\Entity\Group) { 'name' => 'test group', '[new]' => true, '[accessible]' => [ 'name' => true, 'editors_groups' => true, 'groups_writers' => true ], '[dirty]' => [ 'name' => true ], '[original]' => [], '[virtual]' => [], '[hasErrors]' => false, '[errors]' => [], '[invalid]' => [], '[repository]' => 'Groups' } 不知道将patchEntityeditors的主键与writers中的行进行匹配。

例如,将行手动添加到联接表后

users

进入INSERT INTO `editors_groups` (`id`, `user_id`, `group_id`) VALUES ('1', '1', '1'); /users/edit/1将显示正确的关联。


修改

添加必需的可访问字段后,关联才能工作。

/groups/view/1

要使用class User extends Entity { protected $_accessible = [ 'username' => true, 'editor_groups' => true, 'writer_groups' => true ]; } class Group extends Entity { protected $_accessible = [ 'name' => true, 'editors' => true, 'writers' => true ]; } ,我添加了以下内容:

through
class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('EditorGroups')
            ->setClassName('Groups')
            ->setForeignKey('user_id')
            ->setTargetForeignKey('group_id')
            ->setThrough('EditorsEditorGroups');

        $this->belongsToMany('WriterGroups')
            ->setClassName('Groups')
            ->setForeignKey('user_id')
            ->setTargetForeignKey('group_id')
            ->setThrough('WritersWriterGroups');
    }
}

class GroupsTable extends Table
{
    public function initialize(array $config)
    {
        $this->belongsToMany('Editors')
            ->setClassName('Users')
            ->setForeignKey('group_id')
            ->setTargetForeignKey('user_id')
            ->setThrough('EditorsEditorGroups');

        $this->belongsToMany('Writers')
            ->setClassName('Users')
            ->setForeignKey('group_id')
            ->setTargetForeignKey('user_id')
            ->setThrough('WritersWriterGroups');
    }
}

0 个答案:

没有答案