Doctrine Module objectSelect setvalue不起作用

时间:2015-06-14 14:55:26

标签: doctrine-orm zend-framework2

我有一个关于Doctrine Modules Object Select的简单问题。

我有一个简单的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);

有谁知道这是为什么?

非常感谢提前。

1 个答案:

答案 0 :(得分: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');