Пример #1
0
 /**
  * Retrieve columns and primary keys definition array for create table
  *
  * @param Table $table
  * @return string[]
  * @throws ErrorException
  */
 protected function _getColumnsDefinition(Table $table)
 {
     $definition = array();
     $primary = array();
     $columns = $table->getColumns();
     if (empty($columns)) {
         throw new ErrorException('Table columns are not defined');
     }
     foreach ($columns as $columnData) {
         $columnDefinition = $this->_getColumnDefinition($columnData);
         if ($columnData['PRIMARY']) {
             $primary[$columnData['COLUMN_NAME']] = $columnData['PRIMARY_POSITION'];
         }
         $definition[] = sprintf('  %s %s', $this->quoteIdentifier($columnData['COLUMN_NAME']), $columnDefinition);
     }
     // PRIMARY KEY
     if (!empty($primary)) {
         asort($primary, SORT_NUMERIC);
         $primary = array_map(array($this->getPlatform(), 'quoteIdentifier'), array_keys($primary));
         $definition[] = sprintf('  PRIMARY KEY (%s)', implode(', ', $primary));
     }
     return $definition;
 }
 /**
  * @param string $mode
  * @param Table $table
  * @param array|int $modifyColumns
  * @throws \Zend_Db_Exception
  */
 private function migrateTable($mode, Table $table, $modifyColumns)
 {
     if ($mode === 'remove' && is_array($modifyColumns)) {
         $describedTable = $this->describeTable($table->getName());
         foreach ($modifyColumns as $column) {
             if (isset($describedTable[$column])) {
                 $cd = $this->getColumnCreateByDescribe($describedTable[$column]);
                 $this->changeColumn($table->getName(), $column, 'z_' . $column, $cd);
             }
         }
         return;
     }
     $modifyColumns = array_flip($modifyColumns);
     // values are now keys
     $modifyColumns = array_change_key_case($modifyColumns, CASE_UPPER);
     $position = '';
     foreach ($table->getColumns() as $name => $column) {
         if (isset($modifyColumns[$name])) {
             $column['AFTER'] = $position;
             switch ($mode) {
                 case 'add':
                     $this->addColumn($table->getName(), $column['COLUMN_NAME'], $column);
                     break;
                 case 'change':
                     $this->changeColumn($table->getName(), $column['COLUMN_NAME'], $column['COLUMN_NAME'], $column);
                     break;
                 default:
                     throw new \InvalidArgumentException('Not implemented in migrateColumn method');
             }
         }
         $position = $column['COLUMN_NAME'];
     }
     return;
 }