CakePHP 3如何使用REST以正确的方式保存相关关联1:N?

CakePHP 3如何使用REST以正确的方式保存相关关联1:N?,cakephp,orm,marshalling,Cakephp,Orm,Marshalling,我想问一下如何在CakePHP3中使用REST保存相关数据 以免假设我有以下关联命令可以有许多相关的标签(请参见下面的EER图) JSON请求结构如下所示: { "command": "whoami", "description": "SOMETHING", "public": false, "tags": [ {"name":"TEST1"}, {"name":"TEST2"} ] } public function

我想问一下如何在CakePHP3中使用REST保存相关数据

以免假设我有以下关联命令可以有许多相关的标签(请参见下面的EER图)

JSON请求结构如下所示:

{
    "command": "whoami",
    "description": "SOMETHING",
    "public": false,
    "tags": [
        {"name":"TEST1"},
        {"name":"TEST2"}
    ]
}
public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('tags');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->belongsTo('Commands', [
            'foreignKey' => 'commands_id',
            'joinType' => 'INNER'
        ]);
    }
我尝试使用

但是没有运气

因此,我采取了以下方法:

public function add()
    {
        $this->request->allowMethod(['post']);
        $data = $this->request->getData();
        $user = $this->CommonHelper->getUserByToken($this->token);
        $command = $this->Commands->newEntity($data);
        $tags = $data['tags'];
        $command->user = $user;
        $command = $this->Commands->patchEntity($command, $data);
        if ($this->Commands->save($command)) {
            if($tags != null)
                $this->addTags($tags, $command);//TODO: DO IT IN THE RIGHT WAY WITH MARSHALLING
            $this->apiResponse['data'] = $command;
            $this->httpStatusCode = 200;
        } else {
            $this->httpStatusCode = 400;
        }
    }

    private function addTags($tags, $command)
    {
        foreach ($tags as $value) {
            $tag = $this->Tags->newEntity($value);
            $tag->commands_id = $command->id;
            $this->Tags->save($tag);
        }
    }
但这是一个唯一的临时解决方案,我希望通过使用CakePHP ORM架构的可能性以正确的方式来实现

标签表定义如下:

{
    "command": "whoami",
    "description": "SOMETHING",
    "public": false,
    "tags": [
        {"name":"TEST1"},
        {"name":"TEST2"}
    ]
}
public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('tags');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->belongsTo('Commands', [
            'foreignKey' => 'commands_id',
            'joinType' => 'INNER'
        ]);
    }

谢谢你的建议。

这是由于模型中的命名约定不好造成的。使用单数外键帮助我解决了这个问题。

您可以用这种方式保存相关数据。 您的数据数组应该是这种格式

$data = array(
    "command"=> "whoami",
    "description"=> "SOMETHING",
    "public"> false,
    "tags": [
        array("name" => "TEST1"),
        array("name"=> "TEST2")
    ]
)

$patchedEntity= $this->Commands->patchEntity($saveData,$data,['associated' => ['Tags']]); 
$result = $this->Commands->save($patchedEntity);
在Commands表中定义关系应该是这样的

public function initialize(array $config)
    {
        parent::initialize($config); 
        $this->setTable('commands');  
        $this->hasMany('Tags', [
            'foreignKey' => 'command_id'
        ]);
    }
public function initialize(array $config)
    {
        parent::initialize($config); 
        $this->setTable('tags');  
        $this->belongsTo('Commands', [
            'foreignKey' => 'command_id'
        ]);
    }
标签表中的关系应该是这样的

public function initialize(array $config)
    {
        parent::initialize($config); 
        $this->setTable('commands');  
        $this->hasMany('Tags', [
            'foreignKey' => 'command_id'
        ]);
    }
public function initialize(array $config)
    {
        parent::initialize($config); 
        $this->setTable('tags');  
        $this->belongsTo('Commands', [
            'foreignKey' => 'command_id'
        ]);
    }

您可以显示您的
标记
表具有与
命令
关联的
belongsTo
。但是
命令
是否与
标记
有相应的
hasMany
关联?不,它没有太多的关联。我认为在控制台烘焙模型期间,应该根据EER方案(SQL结构)自动添加该关联。这意味着使用$this->hasMany('Tags');我可以根据提供的JSON数据自动插入相关表?您的模式不符合要求,外键应该是单键,因此烘焙无法正常工作。除非您明确指定不规则的外键,否则手动添加的关联也不会起作用。谢谢,现在它可以开箱即用。