Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP和tinyint作为布尔值_Php_Cakephp - Fatal编程技术网

CakePHP和tinyint作为布尔值

CakePHP和tinyint作为布尔值,php,cakephp,Php,Cakephp,如何强制CakePHP 2.x检索tinyint数据库列而不是作为布尔值而是作为tinyint MySQL: Column | type ------------------------------- ... | ... category_id | tinyint(1) ... | ... CakePHP: $this->request->data = $this->Question->r

如何强制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)。

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

如果有人仍然存在此问题,则正确的方法(CakePHP3)

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;
}

这里为cakephp4让路

use Cake\Database\Schema\TableSchema;

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

        return $schema;
    }
}

什么意思?对于0或1,它检索该值,而不是将其转换为布尔值的php(但这取决于您);例如,如果int是9,它会变成什么?。无论如何,您可以将其强制转换为int:
intval($result)
(int)$result
MySQL tinyin不接受从-128到127的值,我使用此类型在表中存储类别id。当我要求分类时,例如12,它检索1…你应该发布你正在使用的代码哦,所以它是tinyint(1)你正在使用…这不意味着ID在1位数之后都被截断了吗?MySQL 8已经不推荐使用整数显示长度。在phpMyAdmin中更改列时,可能会无意中重置tinyint并混淆CakePHP 2。运行:
ALTER TABLE`yourtable`CHANGE`yourfield``yourfield`TINYINT(2)无符号非空有趣,对吗?如果他使用
TINYINT(1)
这不意味着他的所有类别ID都会被截断吗?在这一点上没有问题,因为CakePHP无论如何都没有正确的数据来获取。如果他将ID存储在一个小容器中,它无论如何都会截断它们。tinyint实际上只适用于布尔人(就蛋糕而言),不适用于FK。正如我对Damien Pirsy所说的,tinyint的范围是255。我有15个类别,所以我认为这已经足够了。有什么我遗漏了吗?@BaptisteCosta Tinyint有这个范围,除非你限制它,因为你这么做是对的——这是唯一的解决办法。也是在cake中处理此类数据的正确方法!请检查此处:如果您看到\TableSchema导致错误,则应使用\TableSchema接口