2015-06-14 30 views
0

我有一個簡單的問題關於Doctrine模塊對象選擇。學說模塊objectSelect setvalue不起作用

我有一個簡單objectSelect表單元素

 $this->add(array(
      'name' => 'timezone', 
      'type' => 'DoctrineModule\Form\Element\ObjectSelect', 
      'options' => array(
       'label' => _('Timezone:'), 
       'label_attributes' => array('class' => 'required'), 
       'object_manager' => $this->getEntityManager(), 
       'target_class' => 'Application\Entity\Timezones', 
       'property' => 'timezone', 
       'is_method' => true, 
       'find_method' => array(
        'name' => 'FindAll', 
       ), 
      ), 
     )); 

現在我想選擇一個特定的選項爲默認,我已經使用了setValue方法來做到這一點,但它無法正常工作。

$this->get('timezone')->setValue(335); 

有誰知道這是爲什麼?

非常感謝提前。

+0

是否有任何時區與編號335? – danopz

+0

是的,在生成的HTML的選項列表中。 – Garry

回答

0

我想通了它爲什麼不工作。

在我的控制器中,我將表單綁定到一個空的Doctrine實體。這超出了我設定的價值觀。在表單被綁定之後,我在控制器中添加了值,並解決了問題。

$entityManager = $this->getEntityManager(); 
$site = new Sites($this->getServiceLocator()); 
$form = new AddSiteForm($this->getServiceLocator()); 
$form->setHydrator(new DoctrineObject($entityManager)); 
$form->bind($site); 
$form->get('timezone')->setValue(335); 
$form->get('currencyCode')->setValue('GBP');