我想在我的类里面使用 ENUM,但在我的数据库中使用 TINYINT。我关注了这篇文章:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/cookbook/mysql-enums.html
在我的 Mysql 表中:
CREATE TABLE `side` (
`coated` tinyint(1) DEFAULT NULL COMMENT '0 = Uncoated; 1 = Coated',
);
在我的课上:
/**
* @ORM\Column(type="string", columnDefinition="ENUM('coated', 'uncoated')")
*/
private $coated = null;
在 PHP 中运行这些值,我从数据库中获得了真正的值:
0 or 1
我想知道这个解决方案是否适用于 Mysql。希望有解决方案,如果这行不通,我找到的唯一解决方案是:
public function getCoated() {
if ($this->coated === 0){
return "uncoated";
} elseif ($this->coated === 1) {
return "coated";
} else {
return null;
}
}
最佳答案
我建议使用“BIT”数据类型而不是“TINYINT”,这样数据库将只允许将值存储为“0”或“1”以确保安全。
您现有的:
CREATE TABLE `side` (
`coated` tinyint(1) DEFAULT NULL COMMENT '0 = Uncoated; 1 = Coated'
);
将其更改为:
CREATE TABLE `side` (
`coated` BIT DEFAULT NULL COMMENT '0 = Uncoated; 1 = Coated'
);
在“TINYINT”中,它还可以接受 0/1 以外的值,如果有人将您的表数据更新为 2、3、4,这可能会在您的应用程序中产生错误。
关于php - 学说 2 : columnDefinition ENUM not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51136411/