2015-04-23 25 views
1

我們有與其他實體有關係的(OneToMany)用戶實體。在爲用戶實體構建FormType時。我們已將相關實體作爲集合。Symfony2以形式過濾收藏

  $builder->add('sso_users_organization', "collection", array('type'=>new UsersOrganizationType(),'allow_add' => true)); 

我們只想根據狀態「active」顯示關聯實體。

我們嘗試過以下方式進行過濾。

$organizations = $userEntity->getSsoUsersOrganization(); 
foreach($organizations as $key=>$org){ 
    if($org->getStatus() == 0){ 
     unset($organizations[$key]); 
    } 
} 

但是,當我們正在保存細節時,狀態爲「無效」的其他記錄正在被刪除。

請任何人都可以幫助我。

感謝

回答

0

嘗試這樣的事情。 $ er是由「class」指定的類型的實體存儲庫。在這種情況下UsersOrganizationTypeRepository

$builder->add('sso_users_organization', "entity", array(
    "class" => "Acme\AppBundle\UsersOrganizationType", 
    "query_builder" => function(EntityRepository $er){ 
     return $er->createQueryBuilder('uot') 
      ->where('uot.active = true'); 
    } 
); 
+0

此字段不是選擇元素,包含子表單作爲集合和集合類型我們不能使用query_builder。 – amit

+0

我的不好,不明白那種方式。 – Lunfel