2013-07-17 3 views
0

dev-master에서 symfony 2.2.3 및 소나타 관리를 사용하고 있습니다.SonataUserBundle에서 사용자 그룹을 얻는 방법은 무엇입니까?

사용자 정의 필드를 사용하여 특정 그룹의 사용자를 표시하고 싶습니다.

소나타 사용자 엔터티 및 소나타 그룹 엔터티와의 관계를 만드는 방법을 찾지 못했습니다.

doctrine 스키마는 3 개의 테이블 fos_user_user, fos_user_group 및 관계 테이블 fos_user_user_group을 생성합니다. 내가 쿼리하지 않는 것을 알고

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; 
} 

:

->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(); 
           }, 
          )) 

그리고 내 기능 getCoachs()에서 UserRepository :

내 관리 클래스에서 같은 필드와 양식을했습니다 innerJoin이 올바르지 않기 때문에 작동합니다.

내 사용자 엔티티 및 그룹 엔티티는 BaseUser 및 GroupUser를 id로 확장합니다.

누가 올바른 쿼리를 알고 있습니까?

감사합니다.

편집 : 쿼리에 대한 해결책을 발견했습니다 확인

그러나

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; 
} 

을, 나는이 오류했습니다 : 이해가 안

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

을하기 때문에 쿼리 빌더를 반환하는 내 함수 getCoachs()를 반환하십시오 ...

모름?

답변

2

좋아 해결책을 찾았습니다!

내 쿼리는 다음과 같습니다

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; 
} 

는 사실, 특정 열 (사용자 이름)의 값을 반환하지만 개체의 사용자를 반환해야합니다.

관련 문제