Beispiel #1
0
 public function testGetFields()
 {
     $table = new Table('table');
     $field1 = new Field('field1', 'type');
     $field2 = new Field('field2', 'type');
     $table->addField($field1);
     $table->addField($field2);
     $fields = $table->getFields();
     $this->assertEquals(array('field1' => $field1, 'field2' => $field2), $fields);
 }
 /**
  * Creates a new table
  * @param zibo\library\database\definition\Table $table Table definition for the new table
  * @return null
  */
 protected function createTable(Table $table)
 {
     $tableName = $this->connection->quoteIdentifier($table->getName());
     $fields = $table->getFields();
     $primaryKeys = array();
     $indexes = array();
     $uniques = array();
     $sql = '';
     foreach ($fields as $field) {
         $fieldName = $this->connection->quoteIdentifier($field->getName());
         $sql .= $sql == '' ? '' : ', ';
         $sql .= $fieldName;
         $sql .= ' ' . $this->getFieldType($field);
         if ($field->isPrimaryKey()) {
             $primaryKeys[] = $fieldName;
             if ($field->isAutoNumbering()) {
                 $sql .= ' AUTO_INCREMENT';
             }
             continue;
         }
         $sql .= ' DEFAULT ' . $this->getDefaultValue($field);
         if ($field->isUnique()) {
             $uniques[] = $fieldName;
         } elseif ($field->isIndexed()) {
             $indexes[] = $fieldName;
         }
     }
     if ($primaryKeys) {
         $sql .= ', PRIMARY KEY (' . implode(', ', $primaryKeys) . ')';
     }
     if ($uniques) {
         $sql .= ', UNIQUE (' . implode(', ', $uniques) . ')';
     }
     $sql = 'CREATE TABLE ' . $tableName . ' (' . $sql . ') ENGINE=INNODB CHARACTER SET utf8';
     $this->connection->execute($sql);
     foreach ($indexes as $fieldName) {
         $this->addIndexFromFieldName($tableName, $fieldName);
     }
     $indexes = $table->getIndexes();
     foreach ($indexes as $index) {
         $this->addIndex($tableName, $index);
     }
 }