/**
  * Return a better Phalcon type (Check, Select, Password, ...) according to previous Phalcon type
  * and size of the column
  * @param string $type
  * @param \Phalcon\Db\Column $column
  * @return string
  */
 private function _getType($type, $column)
 {
     $size = $column->getSize();
     $columntype = $type;
     if ($type == "Numeric") {
         if ($size == 1) {
             $columntype = "Check";
         } else {
             if ($size == 11) {
                 $columntype = "Select";
             }
         }
     } else {
         if ($type == "Text") {
             if ($size > 255) {
                 $columntype = "Textarea";
             }
             if (preg_match("#(pass|pwd)#i", $column->getName())) {
                 $columntype = "Password";
             }
         }
     }
     return $columntype;
 }
예제 #2
0
 /**
  * Modify table columns.
  *
  * @param string $tableName  Table name.
  * @param string $schemaName Schema name.
  * @param array  $definition Table Definition.
  *
  * @return int
  * @throws \Exception
  */
 protected function _modifyColumns($tableName, $schemaName, $definition)
 {
     if (empty($definition['columns'])) {
         throw new \Exception('Table must have at least one column');
     }
     $counter = 0;
     $db = $this->getDI()->get('db');
     $fields = [];
     foreach ($definition['columns'] as $tableColumn) {
         if (!is_object($tableColumn)) {
             throw new \Exception('Wrong column definition, it must be a object');
         }
         $fields[$tableColumn->getName()] = $tableColumn;
     }
     $localFields = [];
     $description = $db->describeColumns($tableName, $schemaName);
     foreach ($description as $field) {
         $localFields[$field->getName()] = $field;
     }
     foreach ($fields as $fieldName => $tableColumn) {
         if (!isset($localFields[$fieldName])) {
             $db->addColumn($tableName, $tableColumn->getSchemaName(), $tableColumn);
             $counter++;
         } else {
             $currentField = $localFields[$fieldName];
             $changed = false;
             // Hack boolean type.
             if ($currentField->getType() == Column::TYPE_INTEGER && $currentField->getSize() === "1") {
                 $currentField = new Column($fieldName, ['type' => Column::TYPE_BOOLEAN, 'size' => 0, 'notNull' => $currentField->isNotNull(), 'first' => $currentField->isFirst()]);
             }
             if ($currentField->getType() != $tableColumn->getType()) {
                 $changed = true;
             }
             if ($currentField->getSize() != $tableColumn->getSize()) {
                 $changed = true;
             }
             if ($tableColumn->isNotNull() != $currentField->isNotNull()) {
                 $changed = true;
             }
             if ($changed == true) {
                 $db->modifyColumn($tableName, $tableColumn->getSchemaName(), $tableColumn);
                 $counter++;
             }
         }
     }
     foreach (array_keys($localFields) as $fieldName) {
         if (!isset($fields[$fieldName])) {
             $db->dropColumn($tableName, null, $fieldName);
             $counter++;
         }
     }
     return $counter;
 }