2011-07-06 3 views
0

일부 이상한 문제 :MongoDB를 그리고 PHP는 _id를 푼다

객체 마녀가 이미 "_id", MongoDB를은 "_id"를 작성하지 않습니다를 가지고 입력해도 하나 개 propertie 경우 나, 특성을 가진 개체가 첫 번째 물체에요?

// 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

이있어, 난 아무 단서 이유하지만 다음 ID가 설정되어

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

, 그것은 논리적 인 소리 그러나 그것은 작동

일반 배열을 삽입 할 때만 "setId"를 지정하지 않아도됩니다.