Documented example of composite primary key array with DQL throws error #5062

Closed
opened 2026-01-22 14:57:32 +01:00 by admin · 0 comments
Owner

Originally created by @guilliamxavier on GitHub (Mar 24, 2016).

The documentation http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html#primitive-types-only (tutorial Composite and Foreign Keys as Primary Key, section Primitive Types only) states that, given the following entity class (note: syntax error unrelated to this issue fixed as PR #5736):

<?php
namespace VehicleCatalogue\Model;

/**
 * @Entity
 */
class Car
{
    /** @Id @Column(type="string") */
    private $name;
    /** @Id @Column(type="integer") */
    private $year;

    // ...
}

then

for querying you can use arrays to both DQL and EntityRepositories:

  • EntityRepositories:

    $audi = $em->find("VehicleCatalogue\Model\Car", array("name" => "Audi A8", "year" => 2010));
    

    that works: executes the SQL query
    SELECT t0.name AS name1, t0.year AS year2 FROM Car t0 WHERE t0.name = ? AND t0.year = ?
    with params ['Audi A8', 2010].

  • DQL:

    $dql = "SELECT c FROM VehicleCatalogue\Model\Car c WHERE c.id = ?1";
    $audi = $em->createQuery($dql)
               ->setParameter(1, array("name" => "Audi A8", "year" => 2010))
               ->getSingleResult();
    

    that doesn't work: throws an exception Doctrine\ORM\Query\QueryException:
    [Semantical Error] line 0, col 51 near 'id = ?1': Error: Class VehicleCatalogue\Model\Car has no field or association named id
    (from getSingleResult()).
    Tested with all versions from 2.5 down to 2.0.

Is it really possible to use a composite id array with DQL?

Originally created by @guilliamxavier on GitHub (Mar 24, 2016). The documentation http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html#primitive-types-only (tutorial **Composite and Foreign Keys as Primary Key**, section **Primitive Types only**) states that, given the following entity class _(note: syntax error unrelated to this issue fixed as PR #5736)_: > ``` php > <?php > namespace VehicleCatalogue\Model; > > /** > * @Entity > */ > class Car > { > /** @Id @Column(type="string") */ > private $name; > /** @Id @Column(type="integer") */ > private $year; > > // ... > } > ``` then > for querying you can use arrays to both DQL and EntityRepositories: - EntityRepositories: > ``` php > $audi = $em->find("VehicleCatalogue\Model\Car", array("name" => "Audi A8", "year" => 2010)); > ``` that works: executes the SQL query `SELECT t0.name AS name1, t0.year AS year2 FROM Car t0 WHERE t0.name = ? AND t0.year = ?` with params `['Audi A8', 2010]`. - DQL: > ``` php > $dql = "SELECT c FROM VehicleCatalogue\Model\Car c WHERE c.id = ?1"; > $audi = $em->createQuery($dql) > ->setParameter(1, array("name" => "Audi A8", "year" => 2010)) > ->getSingleResult(); > ``` that **_doesn't work:_** throws an exception `Doctrine\ORM\Query\QueryException`: **[Semantical Error] line 0, col 51 near 'id = ?1': Error: Class VehicleCatalogue\Model\Car has no field or association named id** (from `getSingleResult()`). Tested with all versions from 2.5 down to 2.0. Is it really possible to use a composite id array with DQL?
admin closed this issue 2026-01-22 14:57:32 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5062