一些奇怪的问题:
我有一个带有属性的Object,如果一个属性是另一个已经有“_id”的对象,mongoDB不会为第一个对象创建“_id”吗?
// convert properties to array
$save = $this->model->toArray();
foreach ($save as $key => $val) {
// value is an object, convert to array
if ($val instanceof NcX_Mongo_Model) {
$save[$key] = $val->toArray();
}
}
// update
if ($this->model->getId()) {
$this->collection->save($save);
} else {
error_log('insert');
// insert
$this->collection->insert($save);
}
更多代码..:
$event = new My_Model_Mongo_Event();
$location = new My_Model_Mongo_Location();
$event->setLocation($location);
$event->save();
// no id set
$event->setLocation($location->toArray());
$event->save();
// everything works.. and id is set to event
答案 0 :(得分:0)
知道了,我不知道为什么会这样做:
// update
if ($this->model->getId()) {
$this->collection->save($save);
} else {
// insert
$this->collection->insert($save);
// if i dont do this, the model has no id when inserting 2 objects
$this->model->setId($save['_id']);
}
ID已设置,听起来合乎逻辑但只在插入普通数组时没有“setId”。