for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* Base class for migrations that add a column to a table.
*
* @author Sam Stenvall <[email protected]>
* @copyright Copyright © Sam Stenvall 2014-
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
*/
abstract class AddColumnMigration extends CDbMigration
{
* @return string
abstract protected function getTableName();
abstract protected function getColumnName();
abstract protected function getColumnType();
public function up()
$table = Yii::app()->db->schema->getTable($this->getTableName());
if ($table === null)
return false;
// Check if column already exists
$columns = $table->getColumnNames();
if (!in_array($this->getColumnName(), $columns))
$this->addColumn($this->getTableName(), $this->getColumnName(), $this->getColumnType());
}
public function down()
$this->dropColumn($this->getTableName(), $this->getColumnName());
// Clear the schema cache for this table
$this->refreshTableSchema($this->getTableName());