Example #1
0
 public function __construct(array $createStatementLines, $tableName, Garp_Spawn_Model_Abstract $model)
 {
     $this->setTableName($tableName);
     $this->_model = $model;
     foreach ($createStatementLines as $line) {
         if (Garp_Spawn_MySql_ForeignKey::isForeignKeyStatement($line)) {
             $this->foreignKeys[] = new Garp_Spawn_MySql_ForeignKey($line);
         } elseif (Garp_Spawn_MySql_UniqueKey::isUniqueKeyStatement($line)) {
             $this->uniqueKeys[] = new Garp_Spawn_MySql_UniqueKey($line);
         } elseif (Garp_Spawn_MySql_PrimaryKey::isPrimaryKeyStatement($line)) {
             $this->primaryKey = new Garp_Spawn_MySql_PrimaryKey($line);
         }
     }
     //  now retrieve index keys, excluding foreign keys
     foreach ($createStatementLines as $line) {
         if (Garp_Spawn_MySql_IndexKey::isIndexKeyStatement($line, $this->foreignKeys)) {
             $this->indices[] = new Garp_Spawn_MySql_IndexKey($line);
         }
     }
 }
Example #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;
 }
Example #3
0
 protected function _addIndexForForeignKey(Garp_Spawn_MySql_ForeignKey $key)
 {
     $indexKeySql = Garp_Spawn_MySql_IndexKey::renderSqlDefinition($key->localColumn);
     $indexKey = new Garp_Spawn_MySql_IndexKey($indexKeySql);
     $tableName = $this->getSource()->getTableName();
     if (!Garp_Spawn_MySql_IndexKey::add($tableName, $indexKey)) {
         throw new Exception("Could not create '{$key->localColumn}' index key.");
     }
 }