2014-11-25 128 views
2

我有一個集合字段類型的窗體。 我想過濾它,因爲我們可以爲實體字段類型做,但我找不到解決方案。symfony過濾器集合字段類型像實體字段類型

我發現其他類似的問題,但目前爲止沒有滿意的答案。我們可以這樣做:

$builder 
    ->add('userIngredients', 'collection', array(
      'type' => new UserImportedIngredientType($this->userIngredients), 
      'query_builder'=>$this->queryBuilder, 
     )) 
; 

如果沒有,我們可以使用的形式監聽事件排除基於對象屬性的一些元素?怎麼樣 ?

此集合代表userIngredients,我希望用戶能夠更改它們的屬性isImported設置爲true,因此可以搜索query_builder解決方案。

回答

1

嗯,我想我可以做簡單的東西作爲建立定期形式不附給母公司。

萬一這可能幫助別人:

class UserImportedIngredientType extends AbstractType 
{ 

    protected $userIngredients; 
    protected $userImportedIngredients; 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     foreach ($this->userImportedIngredients as $userImportedIngredient) 
     { 
      /** 
      * @var $userImportedIngredient UserIngredient 
      */ 
      $builder 
       ->add($userImportedIngredient->getId(), 'genemu_jqueryselect2_entity', array(
         'query_builder'=>$this->userIngredients, 
         'class' => 'AppBundle:FoodAnalytics\UserIngredient', 
         'multiple' => false, 
         'label' => $userImportedIngredient->getName(), 
         'required'=>false, 
         'mapped' => false, 
         'data' => $userImportedIngredient 
        )) 
      ; 
     } 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return 'appbundle_foodanalytics_user_imported_ingredient'; 
    } 

    public function __construct($userIngredients, $userImportedIngredients) 
    { 
     $this->userIngredients=$userIngredients; 
     $this->userImportedIngredients=$userImportedIngredients; 
    } 
} 
0

據我所知收集確實not have the query_builder option 所以你不能這樣。 很難破解你想用4行formType做什麼。

你的代碼看起來不錯,除了不支持導入的query_builder,並且你已經將userIngredients傳遞給了構造函數。

我的是,如果你需要過濾這個,那麼你不應該在集合類型,但在其他地方。

編輯:

在第二,雖然,你正試圖在錯誤的地方來篩選集合。是不是在基地集合,但在:

class UserImportedIngredientType extends AbstractType { 
    function __construct($userIngredients) { 
    // Do your stuff 
    } 

    function buildForm(FormBuilderInterface $builder, array $options) { 
    // Add here your entity with the query_filter option :) 

    } 
} 

檢查中的其他條目,以便在集合,有many of them

1

我與另外索納塔解決它像這樣在我的情況上Symfony的頂部。我所做的是將'數據'參數作爲實體結果的具體原則查詢數組。 (在庫:返回$ queryBuilder-> getQuery() - >的getResult();

/** @var MyEntityRepository $myEntityRepository */ 
$myEntityRepository = $this->getEntityManager()->getRepository(MyEntity::class); 
/** @var MyEntity[] $myEntities */ 
$myEntities = $myEntityRepository->findBySomeCriteriaFilter(
    $parameter, Constants::specificConstant 
); 

$formMapper->add(
    // html name= 
    'myEntityProperty', 
    \Symfony\Component\Form\Extension\Core\Type\CollectionType::class, 
    [ 
     // specific form for MyEntity 
     'entry_type' => new Form\MyEntity\MyEntityType(), 
     'allow_add' => true, 
     'label' => false, 
     'entry_options' => [ 
      'label' => false 
     ], 
     // the filtered array of entities from doctrine repository query above 
     'data' => $myEntities, 
     ] 
); 

用此代替sonata_type_model_list,我猜。 此外,如果您想過濾sonata_type_model,請改用EntityType,並使用Closure中的'query_builder'選項並返回queryBuilder而不是實體數組。這一切都很不一致,最好不要使用symfony和奏鳴曲。