Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP and tinyint as boolean

Tags:

php

cakephp

How can I force CakePHP 2.x to retrieve tinyint database column not as boolean but as tinyint?

MySQL:

Column        |    type
-------------------------------
...           |    ...
category_id   |    tinyint(1)
...           |    ...

CakePHP:

$this->request->data = $this->Question->read();
var_dump($this->request->data['Question']['category']);

The value is always 0 (if the question I'm fetching as the category id 0) or 1 (if the question has any other category id).

like image 278
Baptiste Costa Avatar asked May 29 '12 14:05

Baptiste Costa


3 Answers

Use TINYINT(2) instead. If the length is 1, Cake sees it as a boolean.

like image 118
jeremyharris Avatar answered Sep 29 '22 19:09

jeremyharris


The proper way (CakePHP3), if anyone is still having this problem

Model\UsersTable.php

protected function _initializeSchema( Schema  $schema)
{
    //this is a bigInt(20) field (other same type known Cakephp problem)
    $schema->columnType('OtherField'  , 'string');

    //this is a tinyint field
    $schema->columnType('Type'        , 'integer');

    return $schema;
}
like image 31
Dino Moreira Avatar answered Sep 29 '22 21:09

Dino Moreira


Here giving the way for cakephp4

use Cake\Database\Schema\TableSchema;

class UsersTable extends Table
{
    protected function _initializeSchema(TableSchema $schema)
    { 
        $schema->setColumnType('my_column', 'integer');

        return $schema;
    }
}
like image 42
Mushfiqur Rahman Avatar answered Sep 29 '22 21:09

Mushfiqur Rahman