Entities with custom DBAL type identifier bound to DQL query parameters are not converted through DBAL types #6489

Open
opened 2026-01-22 15:34:01 +01:00 by admin · 1 comment
Owner

Originally created by @Ocramius on GitHub (Jun 11, 2020).

Bug Report

Binding DQL query parameters to entities with primary key being mapped with a custom DBAL type does not lead to the identifiers being passed through the DBAL type conversion when the query is executed.

Q A
BC Break no
Version 2.7.3

Summary

Given:

class MyRot13Type extends Type { ... }

and

class MyEntity {
    private Rot13Value $id;
}

When calling:

$entity = new MyEntity();
$em->persist($entity);
$em->flush();

assert(
    null !== $em->createQuery('SELECT e FROM MyEntity e WHERE e = :id')
        ->setParameter('id', $entity)
        ->getOneOrNullResult()
);

Current behavior

The above will report zero results.

Expected behavior

The above should report one result

Originally created by @Ocramius on GitHub (Jun 11, 2020). ### Bug Report Binding DQL query parameters to entities with primary key being mapped with a custom DBAL type does not lead to the identifiers being passed through the DBAL type conversion when the query is executed. | Q | A |------------ | ------ | BC Break | no | Version | 2.7.3 #### Summary Given: ```php class MyRot13Type extends Type { ... } ``` and ```php class MyEntity { private Rot13Value $id; } ``` When calling: ```php $entity = new MyEntity(); $em->persist($entity); $em->flush(); assert( null !== $em->createQuery('SELECT e FROM MyEntity e WHERE e = :id') ->setParameter('id', $entity) ->getOneOrNullResult() ); ``` #### Current behavior The above will report zero results. #### Expected behavior The above should report one result
admin added the Bug label 2026-01-22 15:34:01 +01:00
Author
Owner

@ambroisemaupate commented on GitHub (Nov 29, 2020):

Same issue on 2.8.x-dev

It seems that QueryBuilder tries to serialize object instead of using its primary key:

$qb = $this->createQueryBuilder(static::TRANSLATION_ALIAS);
$qb->select(static::TRANSLATION_ALIAS . '.id')
    ->innerJoin('t.nodeSources', static::NODESSOURCES_ALIAS)
    ->andWhere($qb->expr()->eq(static::NODESSOURCES_ALIAS . '.node', ':node'))
    ->addOrderBy('t.defaultTranslation', 'DESC')
    ->addOrderBy('t.locale', 'ASC')
    ->setParameter('node', $node)
    ->setCacheable(true);
An exception occurred while executing […]
n1_.node_id = ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? 
with params [7, false, false, false, false, false, false, false, false, false, false, false, false]:

To fix this I need to use $node->getId():

$qb = $this->createQueryBuilder(static::TRANSLATION_ALIAS);
$qb->select(static::TRANSLATION_ALIAS . '.id')
    ->innerJoin('t.nodeSources', static::NODESSOURCES_ALIAS)
    ->andWhere($qb->expr()->eq(static::NODESSOURCES_ALIAS . '.node', ':node'))
    ->addOrderBy('t.defaultTranslation', 'DESC')
    ->addOrderBy('t.locale', 'ASC')
    ->setParameter('node', $node->getId())
    ->setCacheable(true);
@ambroisemaupate commented on GitHub (Nov 29, 2020): Same issue on `2.8.x-dev` It seems that QueryBuilder tries to serialize object instead of using its *primary key*: ```php $qb = $this->createQueryBuilder(static::TRANSLATION_ALIAS); $qb->select(static::TRANSLATION_ALIAS . '.id') ->innerJoin('t.nodeSources', static::NODESSOURCES_ALIAS) ->andWhere($qb->expr()->eq(static::NODESSOURCES_ALIAS . '.node', ':node')) ->addOrderBy('t.defaultTranslation', 'DESC') ->addOrderBy('t.locale', 'ASC') ->setParameter('node', $node) ->setCacheable(true); ``` ``` An exception occurred while executing […] n1_.node_id = ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? with params [7, false, false, false, false, false, false, false, false, false, false, false, false]: ``` To fix this I need to use `$node->getId()`: ```php $qb = $this->createQueryBuilder(static::TRANSLATION_ALIAS); $qb->select(static::TRANSLATION_ALIAS . '.id') ->innerJoin('t.nodeSources', static::NODESSOURCES_ALIAS) ->andWhere($qb->expr()->eq(static::NODESSOURCES_ALIAS . '.node', ':node')) ->addOrderBy('t.defaultTranslation', 'DESC') ->addOrderBy('t.locale', 'ASC') ->setParameter('node', $node->getId()) ->setCacheable(true); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6489