Skip to content

Instantly share code, notes, and snippets.

@dcb9
Created March 25, 2016 04:00
Show Gist options
  • Save dcb9/fb48caec99c3e5881ee7 to your computer and use it in GitHub Desktop.
Save dcb9/fb48caec99c3e5881ee7 to your computer and use it in GitHub Desktop.
add comment function for MySQL ColumnSchemaBuilder in yii2 2.0.7
<?php
namespace common\components;
class ColumnSchemaBuilder extends \yii\db\ColumnSchemaBuilder
{
protected $comment;
/**
* @inheritdoc
*/
public function __toString()
{
return
$this->type .
$this->buildLengthString() .
$this->buildUnsignedString() .
$this->buildNotNullString() .
$this->buildUniqueString() .
$this->buildDefaultString() .
$this->buildCheckString() .
$this->buildCommentString();
}
public function buildCommentString()
{
return $this->comment ? " COMMENT '{$this->comment}'" : '';
}
public function comment($comment)
{
$this->comment = $comment;
return $this;
}
}
<?php
return [
'components' => [
'db' => [
// ... ...
// add these three lines
'schemaMap' => [
'mysql' => 'common\components\MysqlSchema',
],
],
],
];
<?php
namespace common\components;
use yii\db\mysql\Schema;
class MysqlSchema extends Schema
{
/**
* @inheritdoc
*/
public function createColumnSchemaBuilder($type, $length = null)
{
return new ColumnSchemaBuilder($type, $length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment