Example #1
0
 /**
  * Allows a field's type, uniqueness, nullability, default, column,  constraints etc. to be modified.
  *
  * Requires a copy of the old field as well so we can only perform changes that are required.
  * If strict is true, raises errors if the old column does not match old_field precisely.
  *
  * @param Model      $model
  * @param Field      $oldField
  * @param Field      $newField
  * @param bool|false $strict
  *
  * @since 1.1.0
  *
  * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**>
  */
 public function alterField($model, $oldField, $newField, $strict = false)
 {
     $oldType = $oldField->dbType($this->connection);
     $newType = $newField->dbType($this->connection);
     if ($oldType == null && $oldField->relation == null || $newType == null && $newField->relation == null) {
         throw new ValueError(sprintf('Cannot alter field %s into %s - they do not properly define ' . 'db_type (are you using a badly-written custom field?)', $newField->name, $oldField->name));
     } elseif ($oldType == null && $newType == null && ($oldField->relation->through != null && $newField->relation->through != null && $oldField->relation->through->meta->autoCreated && $newField->relation->through->meta->autoCreated)) {
         $this->_alterManyToMany($model, $oldField, $newField, $strict);
     } elseif ($oldType == null && $newType == null && ($oldField->relation->through != null && $newField->relation->through != null && !$oldField->relation->through->meta->autoCreated && !$newField->relation->through->meta->autoCreated)) {
         return;
     } else {
         throw new ValueError(sprintf('Cannot alter field %s into %s - they are not compatible types ' . '(you cannot alter to or from M2M fields, or add or remove through= on M2M fields)', $oldField->name, $newField->name));
     }
     $this->_alterField($model, $oldField, $newField, $strict);
 }