简体   繁体   English

Symfony2和Doctrine2:在Type类中使用Repository类结果

[英]Symfony2 and Doctrine2 : Use a Repository class result in Type class

I have 5 Entities: 我有5个实体:

  1. Affiliation 联系
  2. Person
  3. User 用户
  4. UserAffiliation UserAffiliation
  5. PersonAffiliation PersonAffiliation

My goal is to display a list of choice fields where I can choose all the UserAffiliations which are not in PersonAffiliations. 我的目标是显示一个选择字段列表,我可以选择所有不属于PersonAffiliations的UserAffiliations。

My idea is to create a public function in the UserAffiliationRepository which will return only those affiliations for a specific user which are not preset for a specific person. 我的想法是在UserAffiliationRepository中创建一个公共函数,该函数将仅返回特定用户的那些未为特定人预设的从属关系。

For that, I am using: 为此,我正在使用:

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

And this works fine. 这很好用。

Now, I would like to use this result in a FormBuilder. 现在,我想在FormBuilder中使用此结果。 For that, In my controller, I am using: 为此,在我的控制器中,我正在使用:

$affiliations = $em->getRepository('SciForumVersion2Bundle:UserAffiliation')->getUnselectedAffiliations($user->getId(), $author->getId())
$enquiry    = new PersonAffiliation();
$formType   = new SubmissionAffiliationAddFormType($user, $affiliations);
$form   = $this->createForm($formType, $enquiry);

And then in the Form class, I am using: 然后在Form类中,我正在使用:

$builder->add('affiliation', 'entity', array(
            'class' => 'SciForumVersion2Bundle:UserAffiliation',
            'multiple' => true));

But here, I am getting all the affiliations for the specific user, not only thos ones which are not allready in the PersonAffiliations entity. 但在这里,我获得了特定用户的所有从属关系,而不仅仅是那些尚未在PersonAffiliations实体中使用的关联。

Any help? 有帮助吗? Thank you. 谢谢。

You have to migrate your getUnselectedAffiliations function directly into entity_type in the following way 您必须按以下方式将getUnselectedAffiliations函数直接迁移到entity_type中

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

if you want to pass $yourParameters , you have to do that into __construct function (implement it, if you don't have one) and when you create your form, you can pass them along the calls 如果你想传递$yourParameters ,你必须这样做到__construct函数(实现它,如果你没有),当你创建表单时,你可以传递它们的调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM