示例#1
0
 public function testRequiredCheckboxShouldNotBeNullable()
 {
     $field = new Garp_Spawn_Field('config', 'is_highlighted', array('type' => 'checkbox', 'default' => 0, 'required' => true));
     $sql = Garp_Spawn_MySql_Column::renderFieldSql($field);
     $this->assertEquals('  `is_highlighted` tinyint(1) NOT NULL DEFAULT 0', $sql);
     $this->assertEquals(0, $field->default);
 }
示例#2
0
 /**
  * Abstract method to render a CREATE TABLE statement.
  * @param String $modelId   The table name, usually the Model ID.
  * @param Array $fields     Numeric array of Garp_Spawn_Field objects.
  * @param Array $relations  Associative array, where the key is the name
  *                          of the relation, and the value a Garp_Spawn_Relation object,
  *                          or at least an object with properties column, model, type.
  * @param Array $unique     (optional) List of column names to be combined into a unique id.
  *                          This is model-wide and supersedes the 'unique' property per field.
  */
 protected function _renderCreateAbstract($tableName, array $fields, array $relations, $unique)
 {
     $lines = array();
     foreach ($fields as $field) {
         $lines[] = Garp_Spawn_MySql_Column::renderFieldSql($field);
     }
     $primKeys = array();
     $uniqueKeys = array();
     if ($unique) {
         // This checks wether a single one-dimensional array is given: a collection of
         // columns combined into a unique key, or wether an array of arrays is given, meaning
         // multiple collections of columns combining into multiple unique keys per table.
         $isArrayOfArrays = count(array_filter($unique, 'is_array')) === count($unique);
         $unique = !$isArrayOfArrays ? array($unique) : $unique;
         $uniqueKeys = array_merge($uniqueKeys, $unique);
     }
     foreach ($fields as $field) {
         if ($field->primary) {
             $primKeys[] = $field->name;
         }
         if ($field->unique) {
             $uniqueKeys[] = $field->name;
         }
     }
     if ($primKeys) {
         $lines[] = Garp_Spawn_MySql_PrimaryKey::renderSqlDefinition($primKeys);
     }
     foreach ($uniqueKeys as $fieldName) {
         $lines[] = Garp_Spawn_MySql_UniqueKey::renderSqlDefinition($fieldName);
     }
     foreach ($relations as $rel) {
         if (($rel->type === 'hasOne' || $rel->type === 'belongsTo') && !$rel->multilingual) {
             $lines[] = Garp_Spawn_MySql_IndexKey::renderSqlDefinition($rel->column);
         }
     }
     //  set indices that were configured in the Spawn model config
     foreach ($fields as $field) {
         if ($field->index) {
             $lines[] = Garp_Spawn_MySql_IndexKey::renderSqlDefinition($field->name);
         }
     }
     foreach ($relations as $relName => $rel) {
         if (($rel->type === 'hasOne' || $rel->type === 'belongsTo') && !$rel->multilingual) {
             $fkName = Garp_Spawn_MySql_ForeignKey::generateForeignKeyName($tableName, $relName);
             $lines[] = Garp_Spawn_MySql_ForeignKey::renderSqlDefinition($fkName, $rel->column, $rel->model, $rel->type);
         }
     }
     $out = "CREATE TABLE `{$tableName}` (\n";
     $out .= implode(",\n", $lines);
     $out .= "\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
     return $out;
 }
示例#3
0
 public static function renderFieldSql(Garp_Spawn_Field $field)
 {
     $type = Garp_Spawn_MySql_Column::getFieldType($field);
     $reqAndDef = Garp_Spawn_MySql_Column::getRequiredAndDefault($field);
     if ($reqAndDef) {
         $reqAndDef = ' ' . $reqAndDef;
     }
     $autoIncr = $field->name === 'id' ? ' AUTO_INCREMENT' : '';
     return "  `{$field->name}` {$type}{$reqAndDef}{$autoIncr}";
 }
示例#4
0
 protected function _getDiffProperties(Garp_Spawn_MySql_Column $sourceColumn)
 {
     $target = $this->getTarget();
     $targetColumn = $target->getColumn($sourceColumn->name);
     $diffProperties = $sourceColumn->getDiffProperties($targetColumn);
     return $diffProperties;
 }
示例#5
0
 public function alterColumn(Garp_Spawn_MySql_Column $newColumn)
 {
     $alterQuery = "ALTER TABLE `{$this->name}` MODIFY " . $newColumn->renderSqlDefinition();
     if (!$this->_query($alterQuery)) {
         throw new Exception("Could not modify the properties of {$this->name}.{$newColumn->name}\n" . $alterQuery . "\n");
     }
 }