Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Break in 2.8: Cannot bind QueryBuilder parameters with \IteratorAggregate entities: leads to incorrect SQL generated code #8365

@ambroisemaupate

Description

@ambroisemaupate

Bug Report

Binding QueryBuilder query parameters to entities that implements \IteratorAggregate leads to incorrect DQL generated code.

Q A
BC Break yes
Version 2.8.x-dev 418587b
Working on 2.7 branch

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

// Node is a valid Doctrine entity with primary key
// Node implements \IteratorAggregate, \Countable
$node = $this->_em->find(Node::class, $nodeId);

// …

$qb = $this->createQueryBuilder('t');
$qb->select('t')
    ->innerJoin('t.nodeSources', 'ns')
    ->andWhere($qb->expr()->eq('ns.node', ':node'))
    ->setParameter('node', $node);

leads into:

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 explicitely use primary key $node->getId():

$qb = $this->createQueryBuilder('t');
$qb->select('t')
    ->innerJoin('t.nodeSources', 'ns')
    ->andWhere($qb->expr()->eq('ns.node', ':node'))
    ->setParameter('node', $node->getId());

#8181

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions