valar morghulis
valar morghulis

Reputation: 2017

How to add tinyint to database field in cakephp 3 using migration Plugin?

I am using cakephp 3 migrations plugin to design database. I want to add a status field tinyint with limit as 1 to an field, I have tried the following but nothing adds up.

Attempt 1. (Fails)

$table->addColumn('status', 'smallinteger', [
        'default' => 0,
        'limit' => 1,
        'null' => false,
]);

Attempt 2. (Fails)

$table->addColumn('status', 'tinyint', [
        'default' => 0,
        'limit' => 1,
        'null' => false,
]);

I could not find any documentation for the same may be its there and i am missing something Docs Link

Upvotes: 4

Views: 5292

Answers (3)

Moby Duck
Moby Duck

Reputation: 2090

It took me a while to figure out how to add a regular, non-boolean tinyint, and I kept coming across this question when searching for an answer. So for the others like me, it's this:

use Phinx\Db\Adapter\MysqlAdapter;
    
$table->addColumn('status', 'integer', [
    'default' => 0,
    'limit' => MysqlAdapter::INT_TINY,
    'null' => false,
]);

Upvotes: 1

Awemo
Awemo

Reputation: 895

The migration plugin uses the Phinx library for executing these updates. Adding tinyint columns should be done with MysqlAdapter::INT_TINY constant as follows:

use Phinx\Db\Adapter\MysqlAdapter;
...
$table->addColumn('status', 'tinyint', [
    'default' => 0,
    'limit' => MysqlAdapter::INT_TINY, // 255
    'null' => false,
]);

Source: Phinx Mysql Tinyint

Upvotes: 1

Nabin  Nembang
Nabin Nembang

Reputation: 753

Adding field type of boolean adds tinyint column of length 1

$table
      ->addColumn('status', 'boolean', [
                'default' => false,
                'null' => false,
            ]);

Upvotes: 8

Related Questions