cakephp-3 hasmany form data save

时间:2014-06-23 09:58:02

标签: cakephp cakephp-3.0 has-many

我有两张桌子简历和链接。简历与链接有很多关系。我想为单个简历保存很多链接。

我的$ this-> request->数据数组就像

Array
(
[alternate_email] => 
[mobile] => 23232
[level] => Student
[youtube] => 
[links] => Array
    (
        [0] => Array
            (
                ['link'] => www.google.com
                ['user_id'] => 1
            )

        [1] => Array
            (
                ['link'] => abc.com
                ['user_id'] => 1
            )

    )

)


$resume = $this->Resumes->newEntity($this->request->data);
if ($this->Resumes->save($resume)) {
// return true;
}

这是有效的,我的数据正确保存在简历表中,但链接表数据保存如下:

id | resume_id | user_id  | link
1  | 1         |          |

我想要

id | resume_id | user_id | link
1  | 1         |         | www.google.com

2 个答案:

答案 0 :(得分:1)

尝试这样做:

$resume = $this->Resumes->newEntity($this->request->data, ['associated' => ['Links']]);

if ($this->Resumes->save($resume, ['associated' => ['Links']])) {
    // return true;
}

答案 1 :(得分:0)

转到Model/Entity/Link.php并将link添加到protected $_accessible数组