2014-04-09 25 views
0

我嘗試了在Google和Google上搜索的所有內容,但仍然沒有運氣。如何將ArrayCollection傳遞給DoctrineModule Form Element ObjectSelect

我有與國家多對多關係用戶的實體,這裏是:

/** 
* @var \Doctrine\Common\Collections\Collection 
* @ORM\ManyToMany(targetEntity="Admin\Entity\Country", cascade={"persist", "remove"}) 
* @ORM\JoinTable(name="user_country_linker", 
*  joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="country_id", referencedColumnName="id")} 
*) 
*/ 
protected $countries; 

現在,我想用允許/只分配各國展示DoctrineModule \表格\元素\ ObjectSelect。我通過調用$ this-> zfcUserAuthentication() - > getIdentity() - > getCountries()來獲得此列表。

有什麼辦法可以將這個ArrayCollection傳遞給ObjectSelect表單元素嗎?

$this->add(array(
     'name' => 'country', 
     'type' => 'DoctrineModule\Form\Element\ObjectSelect', 
     'options' => array(
      'label'    => 'Country', 
      'object_manager' => $em, 
      'target_class'  => '\Admin\Entity\Country', 
      'property'   => 'code', 
      'find_method'  => array(
       'name' => 'findBy', 
       'params' => array(
        'criteria' => array(), 
        'orderBy' => array('id' => 'asc'), 
       ), 
      ), 
      'column-size' => 'sm-10', 
      'label_attributes' => array('class' => 'col-sm-2'), 
      'help-block' => 'Select country where the entity is present' 
     ), 
     'attributes' => array(
      'required' => false 
     ) 
    )); 

非常感謝您的幫助,我真的很感激它!

+0

我asume你在用戶實體的國家。只需使用用戶實體的關係。 – cptnk

+0

您好,感謝您的評論,但我不明白:( 是的'國家'被放置在用戶實體,但我很想設置ObjectSelect的來源爲$ zfcUserIdentity,因爲它保存當前登錄用戶的允許國家 –

+0

@ cptnk我忘了在上面我的評論中加入你...... –

回答

0

如何在控制器中填充下拉列表最好在這裏描述:zf2 create select/drop down box and populate options in controller?。這基本上是AlexP的解決方案。

如果這不是你正在尋找的,也許本文描述的方法可以幫助你。至少它可以幫助其他人一樣我正在尋找這樣一個解決方案:http://samsonasik.wordpress.com/2014/05/22/zend-framework-2-using-doctrinemoduleformelementobjectselect-and-custom-repository/

你基本上創建,其保存自定義查詢來獲取可能的解決方案的定製reposity:

namespace Your\Repository; 


    use Doctrine\ORM\EntityRepository; 

    class CountriesRepository extends EntityRepository 
    { 
     public function getPossibleCountries() 
     { 
      $querybuilder = $this->_em 
           ->getRepository($this->getEntityName()) 
           ->createQueryBuilder('c'); 
      return $querybuilder->select('c')//... define your query here 
         ->getQuery()->getResult(); 
     } 
    } 

那麼你可以參考在ObjectSelect該方法:

  $this->add(array(
       'name' => 'continent', 
       'type' => 'DoctrineModule\Form\Element\ObjectSelect', 
       'options' => array(
        'object_manager'  => $this->entityManager, 
        'target_class'  => 'Your\Entity\User', 
        'property' => 'contries', 
        'is_method' => true, 
        'find_method'  => array(
         'name' => 'getCountries', 
        ), 
       ), 
      )); 
+0

這看起來像我所需要的幾乎完全一樣,但還有一件事 - 我怎樣才能將參數從表單傳遞給getCountries()?基本上,如果我想要使用更多過濾國家的自定義函數? –

相關問題