Doctrine 不会在 Mysql 中使用布尔值和 PDO::ATTR_EMULATE_PREPARES = false 持久化实体
我们正在使用 Symfony 创建一些网络服务.我们使用 Doctrine-ORM 来存储实体,使用 Doctrine-DBAL 来检索数据,因为它非常轻便,并且可以重用 ORM(实体管理器)连接.
We are using Symfony to create some webservices. We use Doctrine-ORM to store entities and Doctrine-DBAL to retrive data because it's very light and can reuse the ORM (entity manager) connection.
当使用 Doctrine-DBAL 时,整数值作为字符串返回给 PHP,我们希望有整数值,特别是因为它们返回到 Javascript.在此讨论之后 如何从 MySQL 获取数字类型使用 PDO? 我们已经安装了 mysql 本机驱动程序 sudo apt-get install php5-mysqlnd
并使用 PDO::ATTR_EMULATE_PREPARE = false 设置了我们的 symfony (dbal) 配置:
When using Doctrine-DBAL, integer values are returned to PHP as strings and we want to have integer values, specially because they are retured to Javascript. Following this discussion How to get numeric types from MySQL using PDO? we have installed mysql native driver sudo apt-get install php5-mysqlnd
and setup our symfony (dbal) configuration with PDO::ATTR_EMULATE_PREPARE = false :
doctrine:
dbal:
.
.
options:
20 : false # PDO::ATTR_EMULATE_PREPARES is 20
使用此配置,当 mysql 字段为整数时,我们将获得整数.到目前为止一切顺利.
With this configuration we are getting integers when mysql fields are integers. So far so good.
但是有一个新问题:当通过 Doctrine-ORM 存储具有布尔值的实体时,实体不会被持久化.我们在日志中看到了 INSERT 和 COMMIT,但该记录不在数据库中(如果我们使用没有在实体中定义布尔字段的表,则会存储该记录).
But there is a new problem: When storing entities with boolean values through Doctrine-ORM the entity is not persisted. We see in the logs the INSERT and the COMMIT, but the record is not in the database (if we use a table with no boolean fields defined in the entity, the record is stored).
此外,我们没有收到任何错误或异常,因此我们发现这非常危险.我们认为 PDO 库中存在错误,但我们必须对其进行更多研究.
Furthermore, we don't get any Error or Exception, so we find this very dangerous. We think there is a bug in the PDO library but we have to look a bit more into it.
问题:有人遇到过这种行为吗?任何解决方法?Doctrine 应该对此负责吗?
The question: Has anybody experienced this behaviour? any workaround? Should Doctrine account for this?
推荐答案
gseric 的 answer 将起作用,但与用整数滋润你的实体的效果.为了仍然在你的实体中获得布尔值,你可以简单地扩展 Doctrine 的 BooleanType
:
gseric's answer will work but with the effect of hydrating your entities with integers. To still get booleans in your entities you can simply extend Doctrine's BooleanType
:
class BooleanToIntType extends DoctrineDBALTypesBooleanType
{
public function getBindingType()
{
return PDO::PARAM_INT;
}
}
然后,在您的应用程序引导程序中:
Then, in your application bootstrap:
DoctrineDBALTypesType::overrideType('boolean', BooleanToIntType::class);
相关文章