简体   繁体   English

在Symfony中使用Doctrine的DBAL检索布尔值

[英]Retrieving boolean values with Doctrine's DBAL in Symfony

I'm using DBAL in a Symfony project to access data in a Mysql database. 我在Symfony项目中使用DBAL来访问Mysql数据库中的数据。 When querying tables with boolean fields (created as tinyint) I get tinyint values in PHP but I would like to get booleans. 当使用布尔字段查询表(创建为tinyint)时,我在PHP中得到tinyint值,但我想得到布尔值。

Somehow, I'd like to get the same mapping as using Doctrine directly. 不知何故,我想直接使用与Doctrine相同的映射。

I thought that the mapping conversion (from mysql to php) was already implemented in DBAL, but I'm not sure if it's suppose to work this way (this layer mapping values back). 我认为映射转换(从mysql到php)已经在DBAL中实现了,但是我不确定它是否会以这种方式工作(这个层映射值返回)。

I have tried registering a custom mapping like the following one, but no success: 我已经尝试注册一个自定义映射,如下所示,但没有成功:

    $this->conn->getDatabasePlatform()->registerDoctrineTypeMapping('tinyint', 'boolean'); 

    $sql = "
    SELECT se.survey_id, se.anonymous
      FROM survey_edition se
    ";
    $stmt = $this->conn->prepare($sql);
    $stmt->execute();

    $result = $stmt->fetch();

In this case 'anonymous' is a tinyint(1) field in Mysql, but I would like that $result['anonymous'] to be a boolean instead of an integer. 在这种情况下,'anonymous'是Mysql中的tinyint(1)字段,但我希望$ result ['anonymous']是布尔值而不是整数。

Do you know if it is possible to get boolean values in PHP from a Mysql query through Doctrine's DBAL? 你知道是否可以通过Doctrine的DBAL从Mysql查询中获取PHP中的布尔值?

Thanks. 谢谢。

Without using some ORM you cannot (as far as I know it) define model types. 如果不使用某些ORM你不能(据我所知)定义模型类型。

Way to resolve this would be to iterate over the data and cast boolean values such as: 解决这个问题的方法是迭代数据并转换布尔值,例如:

$item[$i]['foobar'] = (bool)$item[$i]['foobar']

But this is not even close to ideal solution. 但这甚至不是理想的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM