0

My selection not working, why??? I see others posts, but don't solve this problem.

symfony2 doctrine join

Doctrine Join DQL

If i use join like this:

->join('User', 'u')

The result, is 4 values, where i expected 2...

Models:

class User
{
    ...

    /**
     * @ManyToMany(targetEntity="UserGroup", mappedBy="users", cascade={"all"})
     * @JoinTable(name="users_user_group")
     */
    protected $userGroups;

and

class UserGroup
{
    ...

    /**
     * @ManyToMany(targetEntity="User", inversedBy="userGroups", cascade={"all"})
     * @JoinTable(name="users_user_group")
     **/
    public $users;

QueryBuilder:

$queryBuilder = $this->getController()->getRepository()
        ->createQueryBuilder('ug')
        ->select('ug, u')
        ->leftJoin('User', 'u', 'WITH', 'ug.id = u.userGroups')
        ->andWhere('u.id IN (:ids)')
        ->setParameter('ids', $usersId);

I receive this message:

[Semantical Error] line 0, col 63 near 'userGroups WHERE': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

This SQL work, why DQL not?

SELECT 
    *
FROM
    users_user_group ug
        LEFT JOIN
    user u ON ug.user_id = u.id
WHERE
    ug.user_id IN (1 , 2)
Community
  • 1
  • 1

1 Answers1

0

I changed this line:

->leftJoin('User', 'u', 'WITH', 'ug.id = u.userGroups')

for it:

->join('User', 'u', 'WITH', 'ug.id = u.id')

\o/