Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving boolean values with Doctrine's DBAL in Symfony

I'm using DBAL in a Symfony project to access data in a Mysql database. When querying tables with boolean fields (created as tinyint) I get tinyint values in PHP but I would like to get booleans.

Somehow, I'd like to get the same mapping as using Doctrine directly.

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).

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.

Do you know if it is possible to get boolean values in PHP from a Mysql query through Doctrine's DBAL?

Thanks.

like image 479
Dmt Avatar asked Nov 10 '22 10:11

Dmt


1 Answers

Without using some ORM you cannot (as far as I know it) define model types.

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.

like image 179
Jovan Perovic Avatar answered Nov 14 '22 21:11

Jovan Perovic