/** * 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())); } }