代码之家  ›  专栏  ›  技术社区  ›  redrom

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

  •  0
  • redrom  · 技术社区  · 6 年前

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

    enter image description here

    JSON请求结构如下:

    {
        "command": "whoami",
        "description": "SOMETHING",
        "public": false,
        "tags": [
            {"name":"TEST1"},
            {"name":"TEST2"}
        ]
    }
    

    我尝试使用

    https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-with-associations

    但是没有运气。

    因此,我采用以下方式:

    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架构。

    标签表定义如下:

    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'
            ]);
        }
    

    谢谢你的建议。

    0 回复  |  直到 6 年前
        1
  •  1
  •   Rakesh    6 年前

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

    $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);
    

    在命令表中定义关系应该是这样的。

    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'
            ]);
        }
    
        2
  •  0
  •   redrom    6 年前

    它是由模型中错误的命名约定给出的。外键中使用单数帮助我解决了这个问题。