예제 #1
0
 public function testEncodeDecode_HistoricalDate_Same()
 {
     $model = new TestMongoDateModel();
     $model->date = new DateTime('2001-01-01');
     $encoded = MongoEncoder::encode($model);
     $this->assertIsA($encoded['date'], 'MongoDate');
     $otherModel = new TestMongoDateModel();
     MongoDecoder::decode($otherModel, $encoded);
     $iso8601 = $otherModel->date->format(DateTime::ISO8601);
     $this->assertEqual($model->date, $otherModel->date);
 }
예제 #2
0
 /**
  * @param object $model
  * @param string $id
  * @param int $keyStyle
  * @param string $rootId
  * @param string $property
  * @see ID_IN_KEY
  * @see ID_IN_DOC
  * @return string
  */
 public function write($model, $id, $keyStyle = MongoMapper::ID_IN_KEY, $rootId = '', $property = '')
 {
     CodeGuard::checkTypeAndThrow($rootId, 'string');
     CodeGuard::checkTypeAndThrow($property, 'string');
     CodeGuard::checkTypeAndThrow($id, 'string');
     $data = MongoEncoder::encode($model);
     // TODO Take into account key style for stripping key out of the model if needs be
     if (empty($rootId)) {
         // We're doing a root level update, only $model, $id are relevant
         $id = $this->update($data, $id, self::ID_IN_KEY, '', '');
     } else {
         if ($keyStyle == self::ID_IN_KEY) {
             CodeGuard::checkNullAndThrow($id, 'id');
             $id = $this->update($data, $id, self::ID_IN_KEY, $rootId, $property);
         } else {
             if (empty($id)) {
                 // TODO would be nice if the encode above gave us the id it generated so we could return it to be consistent. CP 2013-08
                 $this->appendSubDocument($data, $rootId, $property);
             } else {
                 $id = $this->update($data, $id, self::ID_IN_DOC, $rootId, $property);
             }
         }
     }
     return $id;
 }