我使用Symfony 2.7、doctrine/orm 2.5 和doctrine-bundle 1.5。在
Cargo
实体中,我有属性 price
:
/**
* @var float
*
* @ORM\Column(name="price", type="float", options={"unsigned":true, "default":0})
*/
protected $price;
在我的每个学说迁移课程中:
$this->addSql('ALTER TABLE cargo CHANGE price price DOUBLE PRECISION DEFAULT \'0\' NOT NULL');
尽管之前已经执行了迁移并且已经设置了价格的默认值。为什么会发生这种情况以及如何解决?或者直接给属性设置默认值更好?根据 Doctrine 文档,这两个选项都可用:
这是因为你的数据库将
0
默认值转换为0.00
。当 \Doctrine\DBAL\Schema\Comparator::compareTables
尝试将当前模式与预期模式进行比较时 - 它发现这些列声明不相同:
NUMERIC(10, 2) DEFAULT '0.00' NOT NULL
vs NUMERIC(10, 2) DEFAULT '0' NOT NULL
(来自 \Doctrine\DBAL\Platforms\AbstractPlatform::columnsEqual
)
所以解决方案是在默认值中使用与数据库相同的格式 -
options={"unsigned":true, "default":"0.00"})