学说模块objectSelect setvalue不起作用

学说模块objectSelect setvalue不起作用

问题描述:

我有一个简单的问题关于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

我想通了它为什么不工作。

在我的控制器中,我将表单绑定到一个空的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');