public function addField($model, $field)
    {
        // many to many
        if ($field->manyToMany && $field->relation->through->getMeta()->autoCreated) {
            $this->createModel($field->relation->through);

            return;
        }
        $type = $field->dbType($this->connection);
        $name = $field->getColumnName();
        $fieldOptions = $this->getDoctrineColumnOptions($field, true);
        // It might not actually have a column behind it
        if (empty($type)) {
            return;
        }

        $tableDef = new TableDiff($model->getMeta()->getDbTable());
        // normal column
        $tableDef->addedColumns[] = new Column(
            $name,
            Type::getType($type),
            $fieldOptions
        );
        // unique constraint
        if ($field->primaryKey) {
            $tableDef->addedIndexes[] = new Index(
                'primary',
                [$name],
                true,
                true
            );
        }
        // unique constraint
        if ($field->isUnique() && !$field->primaryKey) {
            $tableDef->addedIndexes[] = new Index(
                sprintf('uniq_%s_%s', mt_rand(1, 1000), $name),
                [$name],
                true
            );
        }
        // index constraint
        if ($field->dbIndex && !$field->isUnique() && !$field->primaryKey) {
            $tableDef->addedIndexes[] = new Index(
                sprintf('idx_%s_%s', mt_rand(1, 1000), $name),
                [$name]
            );
        }

        // we need to drop in-database defaults
        if ($this->effectiveDefault($field)) {
            //todo
        }

        /* @var $field ForeignKey */
        if ($field->isRelation && $field->relation && $field->dbConstraint) {
            $relField = $field->getRelatedField();

            $tableDef->addedForeignKeys[] = new ForeignKeyConstraint(
                [$field->getColumnName()],
                $relField->scopeModel->getMeta()->getDbTable(),
                [$relField->getColumnName()]
            );
        }

        $this->alterTable($tableDef);
    }
public function addField($model, $field)
    {
        // many to many
        if ($field->manyToMany && $field->relation->through->getMeta()->autoCreated) {
            $this->createModel($field->relation->through);

            return;
        }
        $type = $field->dbType($this->connection);
        $name = $field->getColumnName();
        $fieldOptions = $this->getDoctrineColumnOptions($field, true);
        // It might not actually have a column behind it
        if (empty($type)) {
            return;
        }

        $tableDef = new TableDiff($model->getMeta()->getDbTable());
        // normal column
        $tableDef->addedColumns[] = new Column(
            $name,
            Type::getType($type),
            $fieldOptions
        );
        // unique constraint
        if ($field->primaryKey) {
            $tableDef->addedIndexes[] = new Index(
                'primary',
                [$name],
                true,
                true
            );
        }
        // unique constraint
        if ($field->isUnique() && !$field->primaryKey) {
            $tableDef->addedIndexes[] = new Index(
                sprintf('uniq_%s_%s', mt_rand(1, 1000), $name),
                [$name],
                true
            );
        }
        // index constraint
        if ($field->dbIndex && !$field->isUnique() && !$field->primaryKey) {
            $tableDef->addedIndexes[] = new Index(
                sprintf('idx_%s_%s', mt_rand(1, 1000), $name),
                [$name]
            );
        }

        // we need to drop in-database defaults
        if ($this->effectiveDefault($field)) {
            //todo
        }

        /* @var $field ForeignKey */
        if ($field->isRelation && $field->relation && $field->dbConstraint) {
            $relField = $field->getRelatedField();

            $tableDef->addedForeignKeys[] = new ForeignKeyConstraint(
                [$field->getColumnName()],
                $relField->scopeModel->getMeta()->getDbTable(),
                [$relField->getColumnName()]
            );
        }

        $this->alterTable($tableDef);
    }
public function addField ( $ model , $ field ) { // many to many if ( $ field -> manyToMany && $ field -> relation -> through -> getMeta ( ) -> autoCreated ) { $ this -> createModel ( $ field -> relation -> through ) ; return ; } $ type = $ field -> dbType ( $ this -> connection ) ; $ name = $ field -> getColumnName ( ) ; $ fieldOptions = $ this -> getDoctrineColumnOptions ( $ field , true ) ; // It might not actually have a column behind it if ( empty ( $ type ) ) { return ; } $ tableDef = new TableDiff ( $ model -> getMeta ( ) -> getDbTable ( ) ) ; // normal column $ tableDef -> addedColumns [ ] = new Column ( $ name , Type :: getType ( $ type ) , $ fieldOptions ) ; // unique constraint if ( $ field -> primaryKey ) { $ tableDef -> addedIndexes [ ] = new Index ( 'primary' , [ $ name ] , true , true ) ; } // unique constraint if ( $ field -> isUnique ( ) && ! $ field -> primaryKey ) { $ tableDef -> addedIndexes [ ] = new Index ( sprintf ( 'uniq_%s_%s' , mt_rand ( 1 , 1000 ) , $ name ) , [ $ name ] , true ) ; } // index constraint if ( $ field -> dbIndex && ! $ field -> isUnique ( ) && ! $ field -> primaryKey ) { $ tableDef -> addedIndexes [ ] = new Index ( sprintf ( 'idx_%s_%s' , mt_rand ( 1 , 1000 ) , $ name ) , [ $ name ] ) ; } // we need to drop in-database defaults if ( $ this -> effectiveDefault ( $ field ) ) { //todo } /* @var $field ForeignKey */ if ( $ field -> isRelation && $ field -> relation && $ field -> dbConstraint ) { $ relField = $ field -> getRelatedField ( ) ; $ tableDef -> addedForeignKeys [ ] = new ForeignKeyConstraint ( [ $ field -> getColumnName ( ) ] , $ relField -> scopeModel -> getMeta ( ) -> getDbTable ( ) , [ $ relField -> getColumnName ( ) ] ) ; } $ this -> alterTable ( $ tableDef ) ; }
Creates a field on a model.

Usually involves adding a column, but may involve adding a table instead (for M2M fields).

@param Model $model
@param Field $field

@throws NotImplemented
@throws ValueError
@throws \Doctrine\DBAL\Schema\SchemaException

@since  1.1.0

@author Eddilbert Macharia (http://eddmash.com) <edd.cowan@gmail.com>

@throws \Doctrine\DBAL\DBALException
Creates a field on a model .