0

我有个问题。我在mongodb中有这个db结构:

id:"xxx",
is_validated: "xxx",
validation_code:"xxx",
profile:[
{
        profile_pic:"xxx",
    firstname:"xxx",
    lastname:"xxx",
}
]

我正在使用 cakephp。当我更新记录时,我使用这个:

$this->User->set('id', "xxx");
$this->User->set('profile', array('firstname' => 'Benedict'));
$this->User->save()

当我保存记录时,整个配置文件数组被删除,只保存“名字”:

id:"xxx",
is_validated: "xxx",
validation_code:"xxx",
profile:[
{
    firstname:"xxx"
}
]

我需要能够保存名字而不使用 cakephp 删除 mongodb 的其他数组记录

4

2 回答 2

1

使用 MongoDB 时不遵循标准约定吗?(文档):

// where '1' is the id of your user    
$this->User->read(null, 1);
// set the new value for the field
$this->User->set('profile', array('firstname' => 'Benedict'));
// commit the changes to the database
$this->User->save();

更新

如果上述方法不起作用,请尝试阅读整条记录并进行相应修改:

// set the active record
$this->User->id = 1;
// read the entire record
$user = $this->User->read();
// modify the field
$user['User']['profile']['firstname'] = 'Benedict';
// save the record
$this->User->save($user);
于 2013-05-08T11:11:25.147 回答
0

发生这种情况的原因是您要求使用您传递的数组更新配置文件字段。然后,这会用您的适当替换当前的配置文件数组。

为了解决这个问题,您必须将完整的数组传递给 ie,其中包含您想要保留的键及其值以及您想要更改的键。

于 2013-05-08T10:45:16.213 回答