Doctrine 2: Cannot select entity through identification variables without choosing at least one root entity alias

Benjamin

Rather than an actual question, this is a sticky note for myself, that may help others. There are many other similar questions: 1, 2, 3, 4, 5, 6, but none of them seems to offer this solution.


I have the following entities:

class Order
{
    // ...

    /**
     * @ManyToOne(targetEntity="Customer")
     * @var Customer
     */
    private $customer;

    /**
     * @Column(type="integer")
     * @var int
     */
    private $amount;
}

class Customer
{
    // ...
}

Order has a unidirectional, many-to-one relationship with Customer. I want to get every customer, along with the total amount of his orders, so I run the following DQL query:

SELECT c, SUM(o.amount)
FROM Model\Order o
JOIN o.customer c
GROUP BY c

But I get the following error:

[Doctrine\ORM\Query\QueryException]
[Semantical Error] line 0, col -1 near 'SELECT c, SUM(o.amount)': Error: Cannot select entity through identification variables without choosing at least one root entity alias.

How can I fix it?

Benjamin

This is a known Doctrine limitation.

The solution is to explicitly SELECT the entity you want to retrieve (Customer) and manually join the other entity (Order) from there, using a WITH condition:

SELECT c, SUM(o.amount)
FROM Model\Customer c
JOIN Model\Order o WITH o.customer = c
GROUP BY c

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Doctrine2: Cannot select entity through identification variables without choosing at least one root entity alias

Doctrine2: [Semantical Error] Cannot select entity through identification variables without choosing at least one root entity alias

Cannot select entity through identification variables without choosing at least one root entity alias

Doctrine limit fileds in root entity when doing select with joins?

How do I set up Doctrine 2 with multiple Entity Managers in one Project? (without Symfony/Zend)

Join table in Doctrine without Entity

Doctrine 2 Native Query select from joined entity

Doctrine Symfony2 persist association entity with existing one

Doctrine2 - two fields linked with one entity

Doctrine 2 Join an Entity which joins an Entity

A new entity was found through the relationship - Doctrine

Doctrine: A new entity was found through the relationship

Doctrine "A new entity was found through the relationship" error

Symfony 3, query without Entity but with doctrine

Symfony 3 - Form - CollectionType in Entity without Doctrine

Doctrine: select query to refresh hydrated entity join?

Doctrine return only one parameter of the related entity

symfony and doctrine: two fields pointing to one entity

Zend2 - Doctrine2: Create new entity with Many-to-One relationship

Symfony/Doctrine2 and Associative entity

How to create a table from entity in Doctrine 2

Doctrine 2 Entity Column Method Name

doctrine 2 entity is not filled with childs element

Symfony2 and Doctrine entity undefined method

Symfony2 Doctrine entity not hydrated

Doctrine 2: Inversed self referencing entity

Doctrine 2 custom entity loading and persisting

doctrine2 entity number in month generation

How to make factory in Doctrine 2 entity?