konieckropka
konieckropka

Reputation: 469

TINYINT wrongly converted to NULL instead of boolean in doctrine / symfony2

This is the craziest thing I ever seen. My Symfony2 and Doctrine has gone mad

I have MySQL database with few different fields that are TINYINT. Those are not nullable and all records have those values set to 0 or 1.

All doctrine ORM mapping ale set correctly (I doublechecked it hundred times). All getters are set correctly (doublechecked as well).

But then - for some objects it doesn't work ... some of the TINYINT are not correctly translated to BOOLEAN as it should (and as it works with other objects and with other fields of that enity)... instead it gives NULL - even if in database this TINYINT is set to "1" (or "0" - it doesn't matter).

For some other objects (of the same entity) it works fine TINYINT = "1" is correctly recognized as true and "0" as false boolean.

Those my examples:

Events.orm.xml:

  <entity name="Events" table="events">
    <change-tracking-policy>DEFERRED_IMPLICIT</change-tracking-policy>
    <field name="eventDeleted" type="boolean" column="_event_deleted"/>

Entity: "Events.php"

the mapping:

/**
 * @var boolean $eventDeleted
 *
 * @ORM\Column(name="_event_deleted", type="boolean", nullable=false)
 */
private $eventDeleted;

and getter and setter:

/**
 * Set eventDeleted
 *
 * @param boolean $eventDeleted
 */
public function setEventDeleted($eventDeleted)
{
    $this->eventDeleted = $eventDeleted;
}

/**
 * Get eventDeleted
 *
 * @return boolean
 */
public function getEventDeleted()
{
    return $this->eventDeleted;
}

The database is:

database view

And as I said - for some objects it works and gives "1" or "0" when accessed by for example:

{{ event[0].getEventDeleted }}

and sometimes it gives NULL.

I found some "similar" issue mentioned in doctrine jira, but no guess what solves that and what could be the reason: http://www.doctrine-project.org/jira/browse/DDC-1967

Any idea?

Upvotes: 2

Views: 1624

Answers (1)

Renan Taranto
Renan Taranto

Reputation: 572

Try creating the query "by hand" with the query builder.
I also had the same problem on symfony 2.1 while fetching an entity from the session variable, a boolean field was returning null no matter what value it had. Using the query builder made it work for me.

EDIT
Example:

$qb = $em->createQueryBuilder();  
$query = $qb->select('e.eventDeleted')  
    ->from('BundleName:Events', 'e')  
    ->where('e.id = :id')  
    ->setParameter('id', $id)  
    ->getQuery();  
$eventDeleted = $query->getSingleResult();  

Upvotes: 1

Related Questions