2011-07-06 3 views
0

いくつかの奇妙な問題:のMongoDBとPHPは_idを失い

オブジェクトの魔女はすでに「_id」、MongoDBのは「_id」を作成しませんがありaltough 1 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); 
    } 

より多くのコード..:

// 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が設定されている、それは論理的に聞こえるが、それは動作します:

$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

がそれを手に入れた、私はこれを行うときには、なぜ見当もつかない単純な配列だけを挿入する場合は "setId"を指定しないでください。

関連する問題