![](/img/trans.png)
[英]Symfony2 & Doctrine2 - Class 'Gedmo\Tree\TreeListener' not found
[英]Symfony2 and Doctrine2 : Use a Repository class result in Type class
我有5個實體:
我的目標是顯示一個選擇字段列表,我可以選擇所有不屬於PersonAffiliations的UserAffiliations。
我的想法是在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();
}
}
這很好用。
現在,我想在FormBuilder中使用此結果。 為此,在我的控制器中,我正在使用:
$affiliations = $em->getRepository('SciForumVersion2Bundle:UserAffiliation')->getUnselectedAffiliations($user->getId(), $author->getId())
$enquiry = new PersonAffiliation();
$formType = new SubmissionAffiliationAddFormType($user, $affiliations);
$form = $this->createForm($formType, $enquiry);
然后在Form類中,我正在使用:
$builder->add('affiliation', 'entity', array(
'class' => 'SciForumVersion2Bundle:UserAffiliation',
'multiple' => true));
但在這里,我獲得了特定用戶的所有從屬關系,而不僅僅是那些尚未在PersonAffiliations實體中使用的關聯。
有幫助嗎? 謝謝。
您必須按以下方式將getUnselectedAffiliations
函數直接遷移到entity_type中
$builder->add('affiliation', 'entity', array(
'class' => 'SciForumVersion2Bundle:UserAffiliation',
'multiple' => true,
'query_builder' = function(EntityRepository $repo) use ($yourParameters){
return $repo->createQueryBuilder(....);}));
如果你想傳遞$yourParameters
,你必須這樣做到__construct
函數(實現它,如果你沒有),當你創建表單時,你可以傳遞它們的調用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.