/**
  * @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;
     }
 }
 /**
  * 
  *
  * @return MString
  */
 public function toString()
 {
     if ($this->element()) {
         $markup = new MMutableString();
         $indentString = MString::stringWithRepeatingString(S(" "), $this->indentLevel());
         $properties = new MMutableArray();
         foreach ($this->properties()->allKeys()->toArray() as $name) {
             $value = $this->properties()->objectForKey($name);
             $properties->addObject(Sf("%s=\"%s\"", $name, $value));
         }
         if ($properties->count() > 0) {
             $markup->appendFormat("%s<%s %s", $indentString, $this->element(), $properties->componentsJoinedByString(S(" ")));
         } else {
             $markup->appendFormat("%s<%s", $indentString, $this->element());
         }
         if ($this->text()) {
             $markup->appendFormat(">%s</%s>", $this->text()->stringByEncodingHTMLEntities(), $this->element());
         } else {
             if ($this->subviews()->count() > 0) {
                 $markup->appendLine(S(">"));
                 $markup->appendLine(parent::toString());
                 $markup->appendString(Sf("%s</%s>", $indentString, $this->element()));
             } else {
                 $markup->appendString(S("/>"));
             }
         }
         if ($this->shouldAppendEmptyLine()) {
             $markup->appendLine();
         }
         return $markup;
     } else {
         return parent::toString();
     }
 }
示例#3
0
 /**
  * 
  *
  * @return MArray
  */
 public function componentsSeparatedByString(MString $separator, $allowEmptyStrings = true)
 {
     MAssertTypes('MString', $separator, 'bool', $allowEmptyStrings);
     $stringArray = new MMutableArray();
     $array = explode($separator->stringValue(), $this->stringValue());
     foreach ($array as $str) {
         if ($allowEmptyStrings || !empty($str)) {
             $stringArray->addObject(S($str));
         }
     }
     return $stringArray;
 }
 /**
  * @internal
  *
  * @return void
  */
 protected function parseCommandLineArguments()
 {
     global $argv;
     if ($this->isRunningFromCommandLine()) {
         $command = null;
         $arguments = new MMutableArray();
         foreach ($argv as $argument) {
             if (is_null($command)) {
                 $command = S($argument);
             } else {
                 $arguments->addObject(S($argument));
             }
         }
         $this->_commandName = $command;
         $this->_commandLineArguments = $arguments;
     }
 }
 /**
  * Returns the Object which represents the value for an attribute of this object
  *
  * Attributes are the properties and relationships of this object. This method
  * returns an object or a collection of objects, in the case of a relationship,
  * which represent the value of that attribute
  *
  * @see MManagedObject::setObjectForAttribute()
  * @see MEntityDescriptionAttribute
  * @see MEntityDescriptionProperty
  * @see MEntityDescriptionRelationship
  *
  * @param MEntityDescriptionAttribute $attribute The attribute which you'd like to
  * retrieve the value for
  *
  * @return MObject|MArray An object or an Array containing a collection of Objects
  * that represent the value of the requested attribute
  */
 public function objectForAttribute(MEntityDescriptionAttribute $attribute)
 {
     $object = null;
     $this->fireFault();
     if ($attribute instanceof MEntityDescriptionProperty) {
         $object = $this->updatedData->objectForKey($attribute->name());
         if ($object == null) {
             $object = $this->data->objectForKey($attribute->name());
         }
     } else {
         if ($attribute instanceof MEntityDescriptionRelationship) {
             $objects = new MMutableArray();
             if (($arr = $this->relationships->objectForKey($attribute->name())) != null) {
                 $objects->appendArray($arr);
             }
             if (($arr = $this->insertedRelationships->objectForKey($attribute->name())) != null) {
                 $objects->appendArray($arr);
             }
             if (($arr = $this->removedRelationships->objectForKey($attribute->name())) != null) {
                 $objects->subtractArray($arr);
             }
             if ($attribute->to() == MEntityDescriptionRelationship::ToOne) {
                 $object = $objects->lastObject();
             } else {
                 $object = $objects;
             }
         } else {
             throw new MManagedObjectException($this, S("Unknown attribute type!"));
         }
     }
     return $object;
 }
 /**
  * Saves any unsaved changes in this context and it's Managed Objects into the Persistent Store
  *
  * @see MManagedObjectContext::hasChanges()
  *
  * @return bool Whether or not the save was successful
  */
 public function save()
 {
     if ($this->hasChanges()) {
         $saveRequest = new MSaveRequest($this);
         $insertedObjects = new MMutableArray();
         $updatedObjects = new MMutableArray();
         foreach ($this->managedObjects()->toArray() as $object) {
             if ($object->hasChanges()) {
                 if ($object->objectID() == MManagedObject::UNKNOWN_ID) {
                     $insertedObjects->addObject($object);
                 } else {
                     $updatedObjects->addObject($object);
                 }
             }
         }
         $saveRequest->setInsertManagedObjects($insertedObjects);
         $saveRequest->setUpdateManagedObjects($updatedObjects);
         $saveRequest->setDeleteManagedObjects($this->deletedObjects());
         $affectedObjects = $this->persistentStoreCoordinator()->executeRequest($saveRequest);
         foreach ($affectedObjects->toArray() as $object) {
             if (!$this->deletedObjects->removeObject($object)) {
                 $object->persistChanges();
             }
         }
         return true;
     } else {
         return false;
     }
 }
 /**
  * Returns an Array of all the relationships in this entity
  *
  * The Array returned contains instances of the MEntityDescriptionRelationship
  * class which describes each relationship contained inside this entity
  *
  * @see MEntityDescriptionRelationship
  *
  * @return MArray An array containing all the relationships in this entity
  */
 public function relationships()
 {
     $relationships = new MMutableArray();
     foreach ($this->attributes()->toArray() as $attribute) {
         if ($attribute instanceof MEntityDescriptionRelationship) {
             $relationships->addObject($attribute);
         }
     }
     return $relationships;
 }
 /**
  * @internal
  *
  * @return void
  */
 protected function createDatabaseStructure()
 {
     $this->connection()->beginTransaction();
     $model = $this->persistentStoreCoordinator()->model();
     $tables = new MMutableDictionary();
     $relationshipTables = new MMutableDictionary();
     foreach ($model->entities()->toArray() as $entity) {
         $fields = new MMutableArray();
         $fields->addObject(S("`objectID` INT NOT NULL AUTO_INCREMENT"));
         foreach ($entity->attributes()->toArray() as $attribute) {
             if ($attribute instanceof MEntityDescriptionProperty) {
                 $type = "MEDIUMTEXT";
                 if ($attribute->type() == MEntityDescriptionProperty::IntegerType) {
                     $type = "INT";
                 } else {
                     if ($attribute->type() == MEntityDescriptionProperty::FloatType) {
                         $type = "FLOAT";
                     } else {
                         if ($attribute->type() == MEntityDescriptionProperty::BooleanType) {
                             $type = "TINYINT";
                         } else {
                             if ($attribute->type() == MEntityDescriptionProperty::DateType) {
                                 $type = "INT";
                             } else {
                                 if ($attribute->type() == MEntityDescriptionProperty::BinaryType) {
                                     $type = "LONGBLOB";
                                 }
                             }
                         }
                     }
                 }
                 $fields->addObject(Sf("`%s` %s", $attribute->name(), $type));
             } else {
                 if ($attribute instanceof MEntityDescriptionRelationship) {
                     $tableName = $attribute->tableName();
                     if ($relationshipTables->objectForKey($tableName) == null) {
                         $relationshipFields = new MMutableArray();
                         $relationshipFields->addObject(S("`objectID` INT NOT NULL AUTO_INCREMENT"));
                         $relationshipFields->addObject(Sf("`%s` INT NOT NULL", $attribute->columnName()));
                         $relationshipFields->addObject(Sf("`%s` INT NOT NULL", $attribute->inverseColumnName()));
                         $relationshipTables->setObjectForKey($tableName, $relationshipFields);
                     }
                 }
             }
         }
         $tableName = $entity->plural();
         if ($tables->objectForKey($tableName) == null) {
             $tables->setObjectForKey($tableName, $fields);
         } else {
             throw new MPersistentStoreException(Sf("Duplicate entity name ('%s')!", $entity->name()));
         }
     }
     foreach ($tables->allKeys()->toArray() as $tableName) {
         $tableQuery = $this->createTableQuery($tableName, $tables->objectForKey($tableName), S("objectID"));
         $this->connection()->exec($tableQuery->stringValue());
     }
     foreach ($relationshipTables->allKeys()->toArray() as $tableName) {
         $tableQuery = $this->createTableQuery($tableName, $relationshipTables->objectForKey($tableName), S("objectID"));
         $this->connection()->exec($tableQuery->stringValue());
     }
     $tableQuery = $this->createTableQuery(S("Z_METADATA"), A(S("`id` INT NOT NULL AUTO_INCREMENT"), S("`key` MEDIUMTEXT"), S("`value` MEDIUMTEXT")), S("id"));
     $this->connection()->exec($tableQuery);
     $statement = $this->connection()->prepare("INSERT INTO `Z_METADATA` (`key`, `value`) VALUES ('version', ?)");
     $statement->execute(array($this->persistentStoreCoordinator()->model()->version()->stringValue()));
     $success = $this->connection()->commit();
     if (!$success) {
         throw new MPersistentStoreException(S("Could not create database structure!"));
     }
     if ($this->delegate() != null) {
         $this->delegate()->didCreatePersistentStore($this);
     }
 }