LazyRecord\Schema\DeclareSchema::column PHP Метод

column() публичный Метод

Define new column object.
public column ( string $name, string $class = 'LazyRecord\Schema\DeclareColumn' ) : DeclareColumn
$name string column name
$class string column class name
Результат DeclareColumn
    public function column($name, $class = 'LazyRecord\\Schema\\DeclareColumn')
    {
        if (isset($this->columns[$name])) {
            throw new Exception("column {$name} of " . get_class($this) . ' is already defined.');
        }
        $this->columnNames[] = $name;
        return $this->columns[$name] = new $class($this, $name);
    }

Usage Example

Пример #1
0
 public function reverseTableSchema($table)
 {
     $tableDef = $this->parseTableSql($table);
     $schema = new DeclareSchema();
     $schema->columnNames = $schema->columns = array();
     $schema->table($table);
     foreach ($tableDef->columns as $columnDef) {
         $name = $columnDef->name;
         $column = $schema->column($name);
         if (!isset($columnDef->type)) {
             throw new LogicException("Missing column type definition on column {$table}.{$name}.");
         }
         $type = $columnDef->type;
         $typeInfo = TypeInfoParser::parseTypeInfo($type, $this->driver);
         $column->type($type);
         if (isset($columnDef->length)) {
             $column->length($columnDef->length);
         }
         if (isset($columnDef->decimals)) {
             $column->decimals($columnDef->decimals);
         }
         $isa = $this->typenameToIsa($type);
         $column->isa($isa);
         if (isset($columnDef->notNull) && $columnDef->notNull !== null) {
             if ($columnDef->notNull) {
                 $column->notNull();
             } else {
                 $column->null();
             }
         }
         if (isset($columnDef->primary)) {
             $column->primary(true);
             $schema->primaryKey = $name;
             if (isset($columnDef->autoIncrement)) {
                 $column->autoIncrement(true);
             }
         } else {
             if (isset($columnDef->unique)) {
                 $column->unique(true);
             }
         }
         if (isset($columnDef->default)) {
             $default = $columnDef->default;
             if (is_scalar($default)) {
                 $column->default($default);
             } else {
                 if ($default instanceof Token && $default->type == 'literal') {
                     $column->default(new Raw($default->val));
                 } else {
                     throw new Exception('Incorrect literal token');
                 }
             }
         }
     }
     return $schema;
 }
All Usage Examples Of LazyRecord\Schema\DeclareSchema::column