Php Symfony2和Doctrine2:在类型类中使用存储库类结果

Php Symfony2和Doctrine2:在类型类中使用存储库类结果,php,symfony,doctrine-orm,dql,Php,Symfony,Doctrine Orm,Dql,我有5个实体: 从属关系 人 使用者 用户关系 人格化 我的目标是显示一个选择字段列表,从中可以选择所有不在PersonalAffiliations中的UserAffiliation 我的想法是在UserAffiliationRepository中创建一个公共函数,该函数将只返回特定用户的附属关系,而这些附属关系不是为特定人员预设的 为此,我使用了: class UserAffiliationRepository extends EntityRepository { public fun

我有5个实体:

  • 从属关系
  • 使用者
  • 用户关系
  • 人格化
  • 我的目标是显示一个选择字段列表,从中可以选择所有不在PersonalAffiliations中的UserAffiliation

    我的想法是在UserAffiliationRepository中创建一个公共函数,该函数将只返回特定用户的附属关系,而这些附属关系不是为特定人员预设的

    为此,我使用了:

    class UserAffiliationRepository extends EntityRepository
    { 
       public function getUnselectedAffiliations( $user_id = null, $person_id = null )
       {
          $commQB = $this->createQueryBuilder( 'ua' )
          ->select('ua');
    
          $commQB->where("ua.user_id = {$user_id}");
    
          $commQB->andWhere( "ua.affiliation_id not in ( select pa.affiliation_id  FROM SciForumVersion2Bundle:PersonAffiliation pa where pa.person_id = 3077 )" );
    
          return $commQB->getQuery()->getResult();
       }
    }
    
    $affiliations = $em->getRepository('SciForumVersion2Bundle:UserAffiliation')->getUnselectedAffiliations($user->getId(), $author->getId())
    $enquiry    = new PersonAffiliation();
    $formType   = new SubmissionAffiliationAddFormType($user, $affiliations);
    $form   = $this->createForm($formType, $enquiry);
    
    $builder->add('affiliation', 'entity', array(
                'class' => 'SciForumVersion2Bundle:UserAffiliation',
                'multiple' => true));
    
    这个很好用

    现在,我想在FormBuilder中使用这个结果。为此,在我的控制器中,我使用:

    class UserAffiliationRepository extends EntityRepository
    { 
       public function getUnselectedAffiliations( $user_id = null, $person_id = null )
       {
          $commQB = $this->createQueryBuilder( 'ua' )
          ->select('ua');
    
          $commQB->where("ua.user_id = {$user_id}");
    
          $commQB->andWhere( "ua.affiliation_id not in ( select pa.affiliation_id  FROM SciForumVersion2Bundle:PersonAffiliation pa where pa.person_id = 3077 )" );
    
          return $commQB->getQuery()->getResult();
       }
    }
    
    $affiliations = $em->getRepository('SciForumVersion2Bundle:UserAffiliation')->getUnselectedAffiliations($user->getId(), $author->getId())
    $enquiry    = new PersonAffiliation();
    $formType   = new SubmissionAffiliationAddFormType($user, $affiliations);
    $form   = $this->createForm($formType, $enquiry);
    
    $builder->add('affiliation', 'entity', array(
                'class' => 'SciForumVersion2Bundle:UserAffiliation',
                'multiple' => true));
    
    然后在Form类中,我使用:

    class UserAffiliationRepository extends EntityRepository
    { 
       public function getUnselectedAffiliations( $user_id = null, $person_id = null )
       {
          $commQB = $this->createQueryBuilder( 'ua' )
          ->select('ua');
    
          $commQB->where("ua.user_id = {$user_id}");
    
          $commQB->andWhere( "ua.affiliation_id not in ( select pa.affiliation_id  FROM SciForumVersion2Bundle:PersonAffiliation pa where pa.person_id = 3077 )" );
    
          return $commQB->getQuery()->getResult();
       }
    }
    
    $affiliations = $em->getRepository('SciForumVersion2Bundle:UserAffiliation')->getUnselectedAffiliations($user->getId(), $author->getId())
    $enquiry    = new PersonAffiliation();
    $formType   = new SubmissionAffiliationAddFormType($user, $affiliations);
    $form   = $this->createForm($formType, $enquiry);
    
    $builder->add('affiliation', 'entity', array(
                'class' => 'SciForumVersion2Bundle:UserAffiliation',
                'multiple' => true));
    
    但是在这里,我得到了特定用户的所有从属关系,而不仅仅是那些在PersonalAffiliations实体中还没有准备好的从属关系


    有什么帮助吗?谢谢。

    您必须以以下方式将
    getUnselectedAffiliations
    函数直接迁移到实体类型

    $builder->add('affiliation', 'entity', array(
                'class' => 'SciForumVersion2Bundle:UserAffiliation',
                'multiple' => true,
                'query_builder' = function(EntityRepository $repo) use ($yourParameters){
                                   return $repo->createQueryBuilder(....);}));
    

    如果您想传递
    $yourParameters
    ,则必须将其传递到
    \uu construct
    函数中(如果您没有,则实现它),并且在创建表单时,可以在调用过程中传递它们

    谢谢@DonCallisto。是的,我有_构造函数,我能够得到所有需要的参数。我现在就试试你的解决方案。嗯,第一个问题,如何访问实体类型类中的
    getUnselectedAffiliations
    ?我应该在createQueryBuilder中输入什么(..)谢谢you@Milos您不必直接将其访问到实体类型中:您必须将代码移动到该函数中,以
    createQuerybuilder(…)
    [替换点]啊,好的,而且不可能在实体类型中直接从存储库访问查询?@Milos我忽略了这种可能性。我想,如果教程说您必须编写DQL或直接在实体类型中构建QueryBuilder,那么这可能是唯一也是最好的解决方案。但我不能打赌:)(你有没有在别处重用这段代码?)