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

Performation

I am stuck with an originally very simple doctrine 2 query. I have an entity called Category which has a OneToMany relation with itself (for parent and child categories).

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
 */
private $parent;

/**
 * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
 */
private $children;

The following query

$q = $this->createQueryBuilder('c')
            ->leftJoin('c.children', 'cc')
            ->select('c.name as title, cc')
            ->where('c.parent IS NULL');

fails with error

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

I don't really get the issue. If I omit the ->select part, the query does work and gives the expected result. I have already searched the forum but couldn't find a solution, that worked. Does anyone have a suggestion? Thanks a lot.

Jason Roman

Your issue is that you are trying to select one field from the Category entity while simultaneously selecting the entire object of the joined Category entity. Unlike plain SQL, with the QueryBuilder component you can't select an entity only from the table you are joining on.

If you are looking to return your main Category object with the joined children, you can either do ->select(array('c', 'cc')), or simply omit the ->select() call altogether. The former will automatically select the children you need in a single query. The latter will require another SQL query if you want to access children on the main Category entity.

If there is a reason you want name to select as title in your object, you can always add another function to your entity that is an alias for retrieving the name instead of having to write it in your query:

function getTitle()
{
    return $this->getName();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Doctrine 2: 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

Doctrine2 - two fields linked with one entity

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

Symfony/Doctrine2 and Associative entity

doctrine2 entity number in month generation

Doctrine2 Entity Custom Joining

Skip persisting of a related entity in Doctrine2

Doctrine2 @ORM in entity mapping annotation

Some logic in Doctrine2 Entity

Doctrine2: get count of one to many relation directly mapped in entity

How to get doctrine2 entity one to many entities that are set "active"

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

symfony2 doctrine2 not flushing new entity

symfony2 doctrine2 batch processing error with entity

Symfony2, Doctrine2, Entity Mapping

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

How to save a clone of an entity as an update with Doctrine2

Doctrine2 entity manager flush not updating the associated entities

How to query in Doctrine2 WHERE = 'value from a related Entity'

Doctrine2 entity creation error : "Could not find driver"

Doctrine2: Generate entities based on entity class

Doctrine2 Class table inheritance Entity ID

Doctrine2 association property order with a name "almost similar" entity

Join table in Doctrine without Entity

Get data through Hibernate without Entity classes

NHibernate: How to eager fetch sub entities with a filter over sub entity through one sql query without lazyload?

A new entity was found through the relationship - Doctrine