DboSource::createSchema PHP Method

createSchema() public method

Generate a database-native schema for the given Schema object
public createSchema ( CakeSchema $schema, string $tableName = null ) : string
$schema CakeSchema An instance of a subclass of CakeSchema
$tableName string Optional. If specified only the table name given will be generated. Otherwise, all tables defined in the schema are generated.
return string
    public function createSchema($schema, $tableName = null)
    {
        if (!$schema instanceof CakeSchema) {
            trigger_error(__d('cake_dev', 'Invalid schema object'), E_USER_WARNING);
            return null;
        }
        $out = '';
        foreach ($schema->tables as $curTable => $columns) {
            if (!$tableName || $tableName === $curTable) {
                $cols = $indexes = $tableParameters = array();
                $primary = null;
                $table = $this->fullTableName($curTable);
                $primaryCount = 0;
                foreach ($columns as $col) {
                    if (isset($col['key']) && $col['key'] === 'primary') {
                        $primaryCount++;
                    }
                }
                foreach ($columns as $name => $col) {
                    if (is_string($col)) {
                        $col = array('type' => $col);
                    }
                    $isPrimary = isset($col['key']) && $col['key'] === 'primary';
                    // Multi-column primary keys are not supported.
                    if ($isPrimary && $primaryCount > 1) {
                        unset($col['key']);
                        $isPrimary = false;
                    }
                    if ($isPrimary) {
                        $primary = $name;
                    }
                    if ($name !== 'indexes' && $name !== 'tableParameters') {
                        $col['name'] = $name;
                        if (!isset($col['type'])) {
                            $col['type'] = 'string';
                        }
                        $cols[] = $this->buildColumn($col);
                    } elseif ($name === 'indexes') {
                        $indexes = array_merge($indexes, $this->buildIndex($col, $table));
                    } elseif ($name === 'tableParameters') {
                        $tableParameters = array_merge($tableParameters, $this->buildTableParameters($col, $table));
                    }
                }
                if (!isset($columns['indexes']['PRIMARY']) && !empty($primary)) {
                    $col = array('PRIMARY' => array('column' => $primary, 'unique' => 1));
                    $indexes = array_merge($indexes, $this->buildIndex($col, $table));
                }
                $columns = $cols;
                $out .= $this->renderStatement('schema', compact('table', 'columns', 'indexes', 'tableParameters')) . "\n\n";
            }
        }
        return $out;
    }

Usage Example

 /**
  * Create a new table.
  *
  * @param string $table
  * @param array $fields
  * @throws TableAlreadyExistsException
  * @throws MigrationException if an sql error occured
  * @return Migration
  */
 public function createTable($table, $fields)
 {
     if (in_array($this->_db->fullTableName($table, false, false), $this->_db->listSources())) {
         throw new TableAlreadyExistsException(__d('migration', 'Table "%s" already exists in database.', $this->_db->fullTableName($table, false, false)));
     }
     $this->_schema->tables = array($table => $fields);
     try {
         $this->_db->execute($this->_db->createSchema($this->_schema));
     } catch (Exception $e) {
         throw new MigrationException(__d('migration', 'SQL Error: %s', $e->getMessage()));
     }
     return $this;
 }
All Usage Examples Of DboSource::createSchema
DboSource