Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set data type as longtext in Yii2 migration

I use following code in order to get desired values of attribute for bodytext. Data type should be longtext() or varchar(). DBMS is MySQL.

With following code bodytext will be created as varchar(255). Why?

$this->createTable('mail_eingang', [
    'id' => $this->primaryKey(),
    'mail_adresse_absender' => $this->string(255)->notNull(),
    'betreff' => $this->string(255),
    'bodytext' => $this->string(), //this will actually set bodytext to varchar(255). It should be longtext() or varchar() !!
    'gelesen' => $this->smallInteger(1)->notNull()->defaultValue(0),
    'angelegt_am' => $this->datetime(),
    'angelegt_von' => $this->integer(11),
    'aktualisiert_am' => $this->datetime(),
    'aktualisiert_von' => $this->integer(11),
    'FOREIGN KEY ([[angelegt_von]]) REFERENCES person ([[id]]) ON DELETE SET NULL ON UPDATE RESTRICT',
    'FOREIGN KEY ([[aktualisiert_von]]) REFERENCES person ([[id]]) ON DELETE SET NULL ON UPDATE RESTRICT',
], $tableOptions);
like image 1000
tklustig Avatar asked Apr 28 '18 11:04

tklustig


1 Answers

If 64 KB is enough for you, you may use text():

'bodytext' => $this->text()

If you really need a longtext column, you should use something like:

'bodytext' => $this->getDb()->getSchema()->createColumnSchemaBuilder('longtext');

Or specify type as raw string:

'bodytext' => 'LONGTEXT',
like image 88
rob006 Avatar answered Oct 08 '22 08:10

rob006