4

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

0

3 Answers 3

8

Adding field type of boolean adds tinyint column of length 1

$table
      ->addColumn('status', 'boolean', [
                'default' => false,
                'null' => false,
            ]);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks man i dind not think that way cakephp take tinyint 1 as boolean
You can also add 'signed' => false to get an unsigned tinyint.
2

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,
]);

Comments

1

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

5 Comments

PHP Fatal error: Class 'MysqlAdapter' not found in /var/www/html/cake3plugin/config/Migrations/20160122144154_CreateProducts.php on line 35
You have to add the classes to be used at the top. use Phinx\Db\Adapter\MysqlAdapter;. Fine if N nem's solution works.
[InvalidArgumentException] An invalid column type "tinyint" was specified for column "status "
appreciate the effort though
There is no tinyint type, you'd have to use integer, and a length of 255. While this would create a TINYINT column, it would have a length of 255, which is not what the OP is looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.