DDC-1001: Join statement fails #1247

Closed
opened 2026-01-22 13:07:09 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Jan 24, 2011).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user rrafal@gmail.com:

I tried to execute this DQL statement:

SELECT u FROM RoomMember m JOIN m.user u WHERE m.room = :roomId

It failed with this error:

class_parents(): object or string expected

Doctrine/ORM/Mapping/ClassMetadataFactory.php:222
Doctrine/ORM/Mapping/ClassMetadataFactory.php:245
Doctrine/ORM/Mapping/ClassMetadataFactory.php:169
Doctrine/ORM/EntityManager.php:247
Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:223
Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:76
Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:98
Doctrine/ORM/AbstractQuery.php:527

I rewrite the query like this, and it works:

SELECT u FROM RoomMember m , User u WHERE m.user = u AND m.room = :roomId

Let me know if you need more information.
Rafal - rrafal@gmail.com

Originally created by @doctrinebot on GitHub (Jan 24, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user rrafal@gmail.com: I tried to execute this DQL statement: SELECT u FROM RoomMember m JOIN m.user u WHERE m.room = :roomId It failed with this error: class_parents(): object or string expected Doctrine/ORM/Mapping/ClassMetadataFactory.php:222 Doctrine/ORM/Mapping/ClassMetadataFactory.php:245 Doctrine/ORM/Mapping/ClassMetadataFactory.php:169 Doctrine/ORM/EntityManager.php:247 Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:223 Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:76 Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:98 Doctrine/ORM/AbstractQuery.php:527 I rewrite the query like this, and it works: SELECT u FROM RoomMember m , User u WHERE m.user = u AND m.room = :roomId Let me know if you need more information. Rafal - rrafal@gmail.com
admin added the Bug label 2026-01-22 13:07:09 +01:00
admin closed this issue 2026-01-22 13:07:11 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jan 25, 2011):

Comment created by @beberlei:

It seems either your mapping files are wrong, can you post the mappings of RoomMember?

@doctrinebot commented on GitHub (Jan 25, 2011): Comment created by @beberlei: It seems either your mapping files are wrong, can you post the mappings of RoomMember?
Author
Owner

@doctrinebot commented on GitHub (Jan 25, 2011):

Comment created by rrafal@gmail.com:

I simplified my code to the following and run the test again:

/****

  • @Entity

  • @Table(name="user")
    /
    class User
    {
    /
    ***

    • @Id
    • @GeneratedValue
    • @Column(name="user_id", type="integer")
      */
      private $id;

    public function **construct() { }
    public function getId(){ return $this->id; }
    }

/****

  • @Entity

  • @Table(name="room_member")
    /
    class RoomMember {
    /
    ***

    • @Id
    • @GeneratedValue
    • @Column(name="room_member_id", type="integer")
      */
      private $id;

    /****

    • @ManyToOne(targetEntity="User")
    • @JoinColumn(name="user_id", referencedColumnName="user_id")
      */
      private $user;

    public function **construct(User $user){
    $this->user = $user;
    }

    /****

    • User who belongs to room.
    • @return User
      */
      public function getUser(){
      return $this->user;
      }
      }

$query = $this->em->createQuery('SELECT u FROM RoomMember m , User u WHERE m.user = u');
$query->execute();// No problem

$query = $this->em->createQuery('SELECT u FROM RoomMember m JOIN m.user u');
$query->execute();// Error

I also tried to recreated this problem using the repository version of doctrine. I created same entities in sandbox environment (doctrine2-orm/tools/sandbox). I'm getting a different error message:

PHP Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message '[Semantical Error] line 0, col -1 near 'SELECT u FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.' in doctrine2-orm/lib/Doctrine/ORM/Query/QueryException.php:47
Stack trace:
#0 doctrine2-orm/lib/Doctrine/ORM/Query/Parser.php(425): Doctrine\ORM\Query\QueryException::semanticalError('line 0, col -1 ...')
#1 doctrine2-orm/lib/Doctrine/ORM/Query/Parser.php(335): Doctrine\ORM\Query\Parser->semanticalError('Cannot select e...')
#2 doctrine2-orm/lib/Doctrine/ORM/Query/Parser.php(281): Doctrine\ORM\Query\Parser->assertSelectEntityRootAliasRequirement()
#3 doctrine2-orm/lib/Doctrine/ORM/Query.php(203): Doctrine\ORM\Query\Parser->parse()
#4 doctrine2-orm/lib/Doctrine/ORM/Query.php(223): Doctrine\ORM\Query->_parse()
#5 /home/rrafal/Develo in doctrine2-orm/lib/Doctrine/ORM/Query/QueryException.php on line 47

I'll attache my code from sandbox.
Thank you for help.

@doctrinebot commented on GitHub (Jan 25, 2011): Comment created by rrafal@gmail.com: I simplified my code to the following and run the test again: /**** - @Entity - @Table(name="user") _/ class User { /_*** - @Id - @GeneratedValue - @Column(name="user_id", type="integer") */ private $id; public function **construct() { } public function getId(){ return $this->id; } } /**** - @Entity - @Table(name="room_member") _/ class RoomMember { /_*** - @Id - @GeneratedValue - @Column(name="room_member_id", type="integer") */ private $id; /**** - @ManyToOne(targetEntity="User") - @JoinColumn(name="user_id", referencedColumnName="user_id") */ private $user; public function **construct(User $user){ $this->user = $user; } /**** - User who belongs to room. - @return User */ public function getUser(){ return $this->user; } } $query = $this->em->createQuery('SELECT u FROM RoomMember m , User u WHERE m.user = u'); $query->execute();// No problem $query = $this->em->createQuery('SELECT u FROM RoomMember m JOIN m.user u'); $query->execute();// Error I also tried to recreated this problem using the repository version of doctrine. I created same entities in sandbox environment (doctrine2-orm/tools/sandbox). I'm getting a different error message: PHP Fatal error: Uncaught exception 'Doctrine\ORM\Query\QueryException' with message '[Semantical Error] line 0, col -1 near 'SELECT u FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.' in doctrine2-orm/lib/Doctrine/ORM/Query/QueryException.php:47 Stack trace: #0 doctrine2-orm/lib/Doctrine/ORM/Query/Parser.php(425): Doctrine\ORM\Query\QueryException::semanticalError('line 0, col -1 ...') #1 doctrine2-orm/lib/Doctrine/ORM/Query/Parser.php(335): Doctrine\ORM\Query\Parser->semanticalError('Cannot select e...') #2 doctrine2-orm/lib/Doctrine/ORM/Query/Parser.php(281): Doctrine\ORM\Query\Parser->assertSelectEntityRootAliasRequirement() #3 doctrine2-orm/lib/Doctrine/ORM/Query.php(203): Doctrine\ORM\Query\Parser->parse() #4 doctrine2-orm/lib/Doctrine/ORM/Query.php(223): Doctrine\ORM\Query->_parse() #5 /home/rrafal/Develo in doctrine2-orm/lib/Doctrine/ORM/Query/QueryException.php on line 47 I'll attache my code from sandbox. Thank you for help.
Author
Owner

@doctrinebot commented on GitHub (Jan 26, 2011):

Comment created by @beberlei:

Ah yes, the query you want to do is not possible. I have only seen that now. You have to select the "m" if you have RoomMember in the FROM clause. That is what the exception in the sandbox is about. Are you using an older version of Doctrine (RC1 or less) for your project? The exception is realtively new

@doctrinebot commented on GitHub (Jan 26, 2011): Comment created by @beberlei: Ah yes, the query you want to do is not possible. I have only seen that now. You have to select the "m" if you have RoomMember in the FROM clause. That is what the exception in the sandbox is about. Are you using an older version of Doctrine (RC1 or less) for your project? The exception is realtively new
Author
Owner

@doctrinebot commented on GitHub (Jan 26, 2011):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jan 26, 2011): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Jan 26, 2011):

Comment created by rrafal@gmail.com:

Ok, thanks for the explanation. I'll just used the alternative DQL, it works perfectly:
SELECT u FROM RoomMember m , User u WHERE m.user = u

I'm using doctrine ORM 2.0.0

@doctrinebot commented on GitHub (Jan 26, 2011): Comment created by rrafal@gmail.com: Ok, thanks for the explanation. I'll just used the alternative DQL, it works perfectly: SELECT u FROM RoomMember m , User u WHERE m.user = u I'm using doctrine ORM 2.0.0
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1247