Exemple #1
0
 /**
  * Removes a field from a model. Usually involves deleting a column, but for M2Ms may involve deleting a table.
  *
  * @param Model $model
  * @param Field $field
  *
  * @since 1.1.0
  *
  * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**>
  */
 public function removeField($model, $field)
 {
     $schema = $this->schemaManager->createSchema();
     // Special-case implicit M2M tables
     if ($field->manyToMany && $field->relation->through->meta->autoCreated) {
         $this->deleteModel($field->relation->through);
         return;
     }
     $type = $field->dbType($this->connection);
     $name = $field->getColumnName();
     $fieldOptions = $this->getDoctrineColumnOptions($field);
     // It might not actually have a column behind it
     if (empty($type)) {
         return;
     }
     $table = $model->meta->dbTable;
     $tableDef = clone $schema->getTable($table);
     // Drop any FK constraints, MySQL requires explicit deletion
     if ($field->isRelation && $field->relation !== null) {
         foreach ($this->constraintName($table, $name, ['foreignKey' => true]) as $fkConstraint) {
             $tableDef->removeForeignKey($fkConstraint);
         }
     }
     // remove column
     $tableDef->dropColumn($name);
     $comparator = new Comparator();
     $diff = $comparator->diffTable($schema->getTable($table), $tableDef);
     if ($diff !== false) {
         $this->schemaManager->alterTable($diff);
     }
 }