Yii2: Mysql longtext in migration

Created on 16 Jun 2016  路  8Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

Create and execute migration with mysql driver

$this->createTable('test_longtext', [
    'id' => $this->primaryKey(),
    'content' => $this->longText(),
]);

What is the expected result?

Must create a table with a field type LONGTEXT.

What do you get instead?

Exception PHP Notice 'yii\base\ErrorException' with message 'Undefined index: LONGTEXT'

Additional info

| Q | A |
| --- | --- |
| Yii version | 2.0.8 |
| PHP version | 5.6.20 |
| Operating system | Debian |

Fix

In class yii\db\ColumnSchemaBuilder replace string 366
from

return $this->categoryMap[$this->type];

to

return isset($this->categoryMap[$this->type]) ? $this->categoryMap[$this->type] : $this->type;

Most helpful comment

So I have checked out the yii2 project source code related to this issue and made a workaround for others facing the same issue.
Please follow described below steps if you want to use additional methods of ColumnSchemaBuilder class such as unique, notNull and others. If you dont need them, you can leave type as plain text as written above.
Warning! This works only on drivers of Mysql and Sqlite.

  • Create a new trait in a separate file with this content:
trait TextTypesTrait
{
       /**
        * @return \yii\db\Connection the database connection to be used for schema building.
         */
        protected abstract function getDb();

       /**
        * Creates a medium text column.
        * @return ColumnSchemaBuilder the column instance which can be further customized.
        */
        public function mediumText()
        {
                return $this->getDb()->getSchema()->createColumnSchemaBuilder('mediumtext');
        }

       /**
        * Creates a long text column.
        * @return ColumnSchemaBuilder the column instance which can be further customized.
        */
        public function longText()
        {
                 return $this->getDb()->getSchema()->createColumnSchemaBuilder('longtext');
        }

       /**
        * Creates a tiny text column.
        * @return ColumnSchemaBuilder the column instance which can be further customized.
        */
        public function tinyText()
        {
                 return $this->getDb()->getSchema()->createColumnSchemaBuilder('tinytext');
        }
}
  • Include this trait inside the class you want a meduimtext or longtext type:
class m... extends Migration
{
    // depends on where you create the trait. I have created it inside project files 'helpers' folder
    use app\helpers\TextTypesTrait;
    ...
}
  • Use methods mediumText() and longText() where necessary:
   // other methods as `notNull`, `unique`, `comment` can be used now
   'mediumtextattribute' => $this->mediumText(),

All 8 comments

@samdark can you tell how to create long text or medium text in this case? I cannot find any documentation on this.

Specify type explicitly.

can you give an example on how to do it, please? I am stuck here and cannot find any in docs.

'text' => 'mediumtext',

So I have checked out the yii2 project source code related to this issue and made a workaround for others facing the same issue.
Please follow described below steps if you want to use additional methods of ColumnSchemaBuilder class such as unique, notNull and others. If you dont need them, you can leave type as plain text as written above.
Warning! This works only on drivers of Mysql and Sqlite.

  • Create a new trait in a separate file with this content:
trait TextTypesTrait
{
       /**
        * @return \yii\db\Connection the database connection to be used for schema building.
         */
        protected abstract function getDb();

       /**
        * Creates a medium text column.
        * @return ColumnSchemaBuilder the column instance which can be further customized.
        */
        public function mediumText()
        {
                return $this->getDb()->getSchema()->createColumnSchemaBuilder('mediumtext');
        }

       /**
        * Creates a long text column.
        * @return ColumnSchemaBuilder the column instance which can be further customized.
        */
        public function longText()
        {
                 return $this->getDb()->getSchema()->createColumnSchemaBuilder('longtext');
        }

       /**
        * Creates a tiny text column.
        * @return ColumnSchemaBuilder the column instance which can be further customized.
        */
        public function tinyText()
        {
                 return $this->getDb()->getSchema()->createColumnSchemaBuilder('tinytext');
        }
}
  • Include this trait inside the class you want a meduimtext or longtext type:
class m... extends Migration
{
    // depends on where you create the trait. I have created it inside project files 'helpers' folder
    use app\helpers\TextTypesTrait;
    ...
}
  • Use methods mediumText() and longText() where necessary:
   // other methods as `notNull`, `unique`, `comment` can be used now
   'mediumtextattribute' => $this->mediumText(),

@iamawebgeek Thanks. That helps a lot.

@samdark how can i use it like this
'content' => $this->longText(),

Was this page helpful?
0 / 5 - 0 ratings

Related issues

spiritdead picture spiritdead  路  67Comments

SamMousa picture SamMousa  路  55Comments

sapsxxxil picture sapsxxxil  路  50Comments

samdark picture samdark  路  63Comments

cebe picture cebe  路  53Comments