/** * Fires the fault fetching process in this object * * If this object is a fault, this method fires it and retreives * the data from Persistent Store therefore taking this object * out of the fault state * * @see MManagedObject::isFault() * * @return void */ public function fireFault() { if ($this->isFault()) { $request = new MFaultRequest($this->context()); $request->addFault($this); if ($this->context()->persistentStoreCoordinator()->executeRequest($request)->count() > 0) { throw new MManagedObjectException($this, S("Error while fetching data for fault!")); } } }
/** * @internal * * @return MArray */ protected function executeFaultRequest(MFaultRequest $request) { $failedFaults = new MMutableArray(); foreach ($request->faults()->toArray() as $fault) { $data = new MMutableDictionary(); $relationships = new MMutableDictionary(); $query = Sf("SELECT * FROM `%s` WHERE `objectID` = :objectID LIMIT 1", $fault->entity()->plural()); $id = $fault->objectID(); $statement = $this->connection()->prepare($query->stringValue()); $statement->bindParam(':objectID', $id, PDO::PARAM_INT); $statement->execute(); $row = $statement->fetch(PDO::FETCH_ASSOC); if ($row != null) { unset($row['objectID']); // Set the object's data foreach ($row as $key => $value) { $property = $fault->entity()->attributeWithName(S($key)); if ($property) { $boxedValue = null; if ($value != null) { if ($property->type() == MEntityDescriptionProperty::StringType) { $boxedValue = new MString($value); } else { if ($property->type() == MEntityDescriptionProperty::IntegerType) { $boxedValue = new MNumber($value); } else { if ($property->type() == MEntityDescriptionProperty::FloatType) { $boxedValue = new MNumber($value); } else { if ($property->type() == MEntityDescriptionProperty::BooleanType) { $boxedValue = new MNumber($value); } else { if ($property->type() == MEntityDescriptionProperty::DateType) { $boxedValue = MDate::parse($value); } else { if ($property->type() == MEntityDescriptionProperty::BinaryType) { $boxedValue = new MData($value); } } } } } } } $data->setObjectForKey(S($key), $boxedValue); } else { throw new MPersistentStoreException(S("Database structure incompatible with this model version!")); } } $fault->_setData($data); foreach ($fault->entity()->relationships()->toArray() as $relationship) { $relationshipQuery = Sf("SELECT * FROM `%s` WHERE `%s` = ?;", $relationship->tableName(), $relationship->columnName()); $objectID = $fault->objectID(); $statement = $this->connection()->prepare($relationshipQuery->stringValue()); $statement->bindParam(1, $objectID); $statement->execute(); $relationshipObjects = new MMutableArray(); foreach ($statement->fetchAll() as $relationshipRow) { $relationshipObject = $this->fetchObjectWithObjectID($this->persistentStoreCoordinator()->model()->entityWithName($relationship->type()), (int) $relationshipRow[$relationship->inverseColumnName()->stringValue()]); if ($relationshipObject) { $relationshipObjects->addObject($relationshipObject); } else { MLog("Warning: Object with ID [%s] is missing from the data store and could not be initialized!", $relationshipRow['objectID']); } } $relationships->setObjectForKey($relationship->name(), $relationshipObjects); } $fault->_setRelationships($relationships); } else { $failedFaults->addObject($fault); } } return $failedFaults; }