/** * 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")); } } } } }
/** * Adds an Object to the specified relationship attribute of this Managed Object * * @param MEntityDescriptionRelationship $relationship The relationship you wish to add * an object to * @param MManagedObject $object The Managed Object to add to the relationship * @param bool $updateInverseRelationship Weather or not the inverse relationship should also be updated * * @return void */ public function addObjectToRelationship(MEntityDescriptionRelationship $relationship, MManagedObject $object, $updateInverseRelationship = true) { MAssertTypes('bool', $updateInverseRelationship); $this->fireFault(); if ($relationship->typeClassName() != $object->className()) { throw new MInvalidManagedObjectOperationException($this, Sf("Invalid type [%s], expected [%s]!", $object->className(), $relationship->typeClassName())); } // Add object on this side of the relationship $arr = $this->insertedRelationships->objectForKey($relationship->name()); if (!$arr) { $arr = new MMutableArray(); $this->insertedRelationships->setObjectForKey($relationship->name(), $arr); } $arr->addObject($object); // Update the inverse side of the relationship if necessary if ($updateInverseRelationship) { $object->addObjectToRelationship($relationship->inverseRelationship(), $this, false); } }
/** * Adds a fault object to the request * * Use this method to add fault objects to your request. This way * you can fetch them all at once and save in performance compared * to fetching them one by one. * * @see MFaultRequest::removeFault() * @see MFaultRequest::faults() * * @param MManagedObject $fault The fault Managed Object to add * * @return void */ public function addFault(MManagedObject $fault) { if (!$fault->isFault()) { throw new MPersistentStoreException(S("Managed Object is not a valid fault")); } $this->faults->addObject($fault); }
/** * @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())); } }
/** * @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())); } }
/** * 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")); } }