Create and execute migration with mysql driver
$this->createTable('test_longtext', [
'id' => $this->primaryKey(),
'content' => $this->longText(),
]);
Must create a table with a field type LONGTEXT
.
Exception PHP Notice 'yii\base\ErrorException' with message 'Undefined index: LONGTEXT'
| Q | A |
| --- | --- |
| Yii version | 2.0.8 |
| PHP version | 5.6.20 |
| Operating system | Debian |
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;
@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.
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');
}
}
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;
...
}
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(),
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 asunique
,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.
meduimtext
orlongtext
type:mediumText()
andlongText()
where necessary: