Cakephp 3.7 hasOne字段数据未保存

时间:2019-01-24 09:29:30

标签: cakephp associations cakephp-3.x

在cakephp 3.7中。在我们的add函数里面。此数据仅保存用户表。个人资料名称字段数据不保存。

$data = [
    'id' => 1,
    'email' => 'cakephp',
    'profile' => [
        'name' => 'cakephp'
    ]
];
$users = TableRegistry::get('Users');
$user = $users->newEntity($data, [
    'associated' => ['Profiles']
]);
$users->save($user);

UsersTable.php

public function initialize(array $config)
{
    parent::initialize($config);

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

    $this->addBehavior('Timestamp');

    $this->hasOne('Profiles');
}

有人可以帮助我吗?为什么配置文件字段数据未保存在配置文件表中

1 个答案:

答案 0 :(得分:0)

通常,您也必须修改实体,以使该字段可访问。

protected $_accessible = [
     ...
    'profiles' => true, //if has many
    'profile' => true, //if has one
];

如果有必要,请不要忘记清除缓存