質問

I'm using symfony 2.2.3 and sonata admin in dev-master.

I would like a custom field to show users from a specific group.

I've not found how to make the relation with Sonata User entity and Sonata Group entity.

The doctrine schema create 3 tables : fos_user_user, fos_user_group and the relation table fos_user_user_group.

I've a form with a field like that in my Admin class :

->add('coach', 'entity', array(
                                'class' => 'ApplicationSonataUserBundle:User',
                                'empty_value' => 'Choisissez un coach',
                                'property' => 'username',
                                'query_builder' => function(\Application\Sonata\UserBundle\Entity\UserRepository $er) {
                                    return $er->getCoachs();
                                },
                            ))

And my function getCoachs() in UserRepository:

public function getCoachs()
{
    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder(array('u', 'g'))
                ->select('u.username, g.id')
                ->from('ApplicationSonataUserBundle:User', 'u')
                ->innerJoin('ApplicationSonataUserBundle:Group', 'g', 'WITH','g.id = u.group_id') 
                ->where('g.id = :id')
                ->setParameter('id', 1);



    return $qb;
}

I know that the query doesn't work because the innerJoin isn't correct.

My user entity and group entity just extends BaseUser and GroupUser with an id.

Someone know the right query ?

Thank you.

EDIT :

Ok i've found the solution for the query :

public function getCoachs()
{
    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder('u')
                ->select('u.username')
                ->from('ApplicationSonataUserBundle:User', 'u')
                ->innerJoin('u.groups', 'g') 
                ->where('g = :id')
                ->setParameter('id', 1);

    return $qb;
}

However, i've this error :

Expected argument of type "object or array", "string" given

I don't understand because I return my function getCoachs() which return a query builder...

No idea ?

役に立ちましたか?

解決

Ok I've found the solution !

My query is :

public function getCoachs()
{
    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder('u')
            ->select('u') // and not u.username
            ->from('ApplicationSonataUserBundle:User', 'u')
            ->innerJoin('u.groups', 'g') 
            ->where('g = :id')
            ->setParameter('id', 1);

    return $qb;
}

In fact, I returned values of a specific column (username) but you have to return the object User.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top