如何强制CakePHP 2.x检索tinyint数据库列不是布尔值而是tinyint?
MySQL的:
Column | type
-------------------------------
... | ...
category_id | tinyint(1)
... | ...
CakePHP的:
$this->request->data = $this->Question->read();
var_dump($this->request->data['Question']['category']);
值始终为0(如果我提取的问题为类别ID 0)或1(如果问题有任何其他类别ID)。
答案 0 :(得分:16)
请改用TINYINT(2)
。如果长度为1,则Cake将其视为布尔值。
答案 1 :(得分:1)
在这里为cakephp4让路
use Cake\Database\Schema\TableSchema;
class UsersTable extends Table
{
protected function _initializeSchema(TableSchema $schema)
{
$schema->setColumnType('my_column', 'integer');
return $schema;
}
}
答案 2 :(得分:0)
正确的方式(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;
}
答案 3 :(得分:0)
下面为我工作,我在获取它之前将其投射。
$result = $this->Question->find('first', array('conditions'=>array('id'=>$id),'fields'=>array('CAST(Question.category AS SIGNED) as category')));
echo $result[0]['category'];