8

如何强制 CakePHP 2.x 检索 tinyint 数据库列,而不是布尔值而是 tinyint?

MySQL:

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

蛋糕PHP:

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

该值始终为 0(如果我作为类别 id 获取的问题 0)或 1(如果问题具有任何其他类别 id)。

4

4 回答 4

17

改为使用TINYINT(2)。如果长度为 1,Cake 将其视为布尔值。

于 2012-05-29T15:07:43.527 回答
3

正确的方法(CakePHP3),如果有人仍然有这个问题

模型\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;
}
于 2017-06-21T23:49:19.460 回答
1

这里给 cakephp4 让路

use Cake\Database\Schema\TableSchema;

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

        return $schema;
    }
}
于 2020-04-21T12:15:45.103 回答
0

下面对我有用。

$result = $this->Question->find('first', array('conditions'=>array('id'=>$id),'fields'=>array('CAST(Question.category AS SIGNED) as category')));
echo $result[0]['category'];
于 2021-07-23T10:58:54.187 回答