Esempio n. 1
0
 /**
  * 
  *
  * @return MDate
  */
 public static function parseString(MString $string)
 {
     return MDate::parse($string->stringValue());
 }
 /**
  * @return MManagedObject
  */
 private function _parseObjectFromXML(SimpleXMLElement $xmlObject, callable $callback = null)
 {
     $entityName = S($xmlObject->getName());
     $entity = $this->persistentStoreCoordinator()->model()->entityWithName($entityName);
     if ($entity) {
         $object = $this->newObjectForEntity($entity);
         if ($object) {
             foreach ($xmlObject->children() as $xmlAttribute) {
                 $attribute = $entity->attributeWithName(S($xmlAttribute->getName()));
                 if ($attribute instanceof MEntityDescriptionProperty) {
                     if ($attribute->type() == MEntityDescriptionProperty::StringType) {
                         $object->setObjectForAttribute($attribute, S((string) $xmlAttribute));
                     } else {
                         if ($attribute->type() == MEntityDescriptionProperty::IntegerType) {
                             $object->setObjectForAttribute($attribute, N((int) $xmlAttribute));
                         } else {
                             if ($attribute->type() == MEntityDescriptionProperty::FloatType) {
                                 $object->setObjectForAttribute($attribute, N((double) $xmlAttribute));
                             } else {
                                 if ($attribute->type() == MEntityDescriptionProperty::BooleanType) {
                                     $object->setObjectForAttribute($attribute, N((bool) $xmlAttribute));
                                 } else {
                                     if ($attribute->type() == MEntityDescriptionProperty::DateType) {
                                         $object->setObjectForAttribute($attribute, MDate::parse((string) $xmlAttribute));
                                     } else {
                                         if ($attribute->type() == MEntityDescriptionProperty::BinaryType) {
                                             $object->setObjectForAttribute($attribute, MData::dataWithBytes((string) $xmlAttribute));
                                         } else {
                                             throw new MInvalidDataTypeException(S("StringType|IntegerType|FloatType|BooleanType|DateType|BinaryType"), S($attribute->type()));
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 } else {
                     if ($attribute instanceof MEntityDescriptionRelationship) {
                         foreach ($xmlAttribute->children() as $xmlRelationshipObject) {
                             $object->addObjectToRelationship($attribute, $this->_parseObjectFromXML($xmlRelationshipObject));
                         }
                     } else {
                         throw new MManagedObjectException($object, Sf("Attribute type '%s' not supported", $attribute->className()));
                     }
                 }
             }
             if (!is_null($callback)) {
                 $callback($object);
             }
             return $object;
         } else {
             throw new MManagedObjectException($object, S("Could not create object"));
         }
     } else {
         throw new MEntityNotFoundException($entityName);
     }
 }
 /**
  * @internal
  *
  * @return bool
  */
 protected function _setValueForField(MApplicationControllerField $field, $value)
 {
     $object = null;
     try {
         if ($field->type() == MApplicationControllerField::StringType) {
             $object = S($value);
         } else {
             if ($field->type() == MApplicationControllerField::IntegerType) {
                 $object = MNumber::parseInt($value);
             } else {
                 if ($field->type() == MApplicationControllerField::FloatType) {
                     $object = MNumber::parseFloat($value);
                 } else {
                     if ($field->type() == MApplicationControllerField::BooleanType) {
                         $object = MNumber::parseBool($value);
                     } else {
                         if ($field->type() == MApplicationControllerField::DateType) {
                             $object = MDate::parse($value);
                         } else {
                             if ($field->type() == MApplicationControllerField::BinaryType) {
                                 $object = MData::parseBase64String(S($value));
                             } else {
                                 if ($field->type() == MApplicationControllerField::ArrayType) {
                                     $object = new MMutableArray();
                                     foreach ($value as $v) {
                                         $object->addObject(S($v));
                                     }
                                 } else {
                                     if ($field->type() == MApplicationControllerField::DictionaryType) {
                                         $object = new MMutableDictionary();
                                         foreach ($value as $k => $v) {
                                             $object->setObjectForKey(S($k), S($v));
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     } catch (Exception $e) {
         return false;
     }
     if (!$this->fieldValues) {
         $this->fieldValues = new MMutableDictionary();
     }
     if ($object) {
         $this->fieldValues->setObjectForKey($field, $object);
         return true;
     } else {
         return false;
     }
 }
 /**
  * @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;
 }