/**
  * Contructs a new XML Managed Object View that represents the object specified in
  * XML format
  *
  * @param MManagedObject $managedObject The Managed Object to be represented as
  * an XML Element View
  *
  * @return MXMLManagedObjectView The new XML Element View instance
  */
 public function __construct(MManagedObject $managedObject)
 {
     parent::__construct($managedObject->entity()->name());
     $this->managedObject = $managedObject;
     $this->dynamicFields = new MMutableDictionary();
     $this->dynamicFieldViews = new MMutableDictionary();
     $this->setValueForProperty(S("objectID"), S((string) $this->managedObject()->objectID()));
     if (!$managedObject->isFault()) {
         foreach ($managedObject->entity()->attributes()->toArray() as $attribute) {
             if ($attribute instanceof MEntityDescriptionProperty) {
                 $object = $managedObject->objectForAttribute($attribute);
                 $propertyElement = new MXMLElementView($attribute->name(), $object ? $object->toString() : S(""));
                 $propertyElement->setValueForProperty(S("type"), S($attribute->type()));
                 $this->addSubview($propertyElement);
             } else {
                 if ($attribute instanceof MEntityDescriptionRelationship) {
                     $relationshipElement = new MXMLElementView($attribute->name());
                     $relationshipElement->setValueForProperty(S("type"), $attribute->type());
                     $relationshipValues = $managedObject->objectForAttribute($attribute);
                     if (!$relationshipValues instanceof MArray) {
                         $relationshipValues = A(array($relationshipValues));
                     }
                     foreach ($relationshipValues->toArray() as $object) {
                         $objectElement = new MXMLElementView($attribute->singular());
                         $objectElement->setValueForProperty(S("objectID"), Sf("%d", $object->objectID()));
                         $relationshipElement->addSubview($objectElement);
                     }
                 } else {
                     throw new MManagedObjectException($managedObject, S("Unsupported Attribute Type"));
                 }
             }
         }
     }
 }
 /**
  * @internal
  *
  * @return void
  */
 protected function deleteManagedObject(MManagedObject $object)
 {
     $object->discardChanges();
     // Remove all object's relationships
     foreach ($object->entity()->relationships() as $relationship) {
         $relationshipObjects = $object->objectForAttribute($relationship);
         if ($relationshipObjects) {
             foreach ($relationshipObjects->toArray() as $relationshipObject) {
                 $object->removeObjectFromRelationship($relationship, $relationshipObject);
             }
         }
     }
     $this->saveManagedObject($object);
     // Remove object from the database
     $query = Sf("DELETE FROM `%s` WHERE `objectID` = ?", $object->entity()->plural());
     $statement = $this->connection()->prepare($query->stringValue());
     if (!$statement->execute(array($object->objectID()))) {
         throw new MPersistentStoreException(Sf("Could not delete entity [%s]", $object->entity()->name()));
     }
 }
 /**
  * @internal
  *
  * Updates a Managed Object with data coming from a POST or PUT request
  *
  * @param MManagedObject $object The managed object instance to be updated
  *
  * @return void
  */
 protected function _updateObjectWithInputData(MManagedObject $object)
 {
     foreach ($object->entity()->properties()->toArray() as $property) {
         $object->setObjectForAttribute($property, $this->valueForFieldNamed($property->name()));
     }
 }
 /**
  * Removes a Managed Object from the entity collection
  *
  * @param MManagedObject $object The object to be removed from the collection
  *
  * @return void
  */
 public function removeManagedObject(MManagedObject $object)
 {
     if ($object->entity()->equals($this->entity())) {
         $elementView = $this->managedObjects->objectForKey($object);
         $this->managedObjects->removeObjectForKey($object);
         $this->removeSubview($elementView);
     } else {
         throw new MManagedObjectException($object, S("Object's entity is different from collections' entity"));
     }
 }