コード例 #1
0
 /**
  * Saves a user to the database.
  * If the user is not saved yet, its uid will be added to the 
  * user_entity.
  * @param User_entity (by reference)
  * 
  * @return boolean
  *   Whether or not the save was successful.
  */
 public function save(User_entity &$entity)
 {
     // To ensure date consistency.
     $date = Mongo_db::date();
     // Set update date:
     $entity->updated = $date;
     if ($entity->author === NULL) {
         $entity->author = current_user()->uid;
     }
     $prepared_data = array();
     foreach ($entity as $field_name => $field_value) {
         $prepared_data[$field_name] = $field_value;
     }
     if ($entity->is_new()) {
         // Add new properties.
         $entity->uid = increment_counter(self::COUNTER_COLLECTION);
         $entity->created = clone $date;
         // Add properties to prepared_data.
         $prepared_data['uid'] = $entity->uid;
         $prepared_data['created'] = $entity->created;
         $result = $this->mongo_db->insert(self::COLLECTION, $prepared_data);
         return $result !== FALSE ? TRUE : FALSE;
     } else {
         $result = $this->mongo_db->set($prepared_data)->where('uid', $entity->uid)->update(self::COLLECTION, array('upsert' => TRUE));
         return $result !== FALSE ? TRUE : FALSE;
     }
 }