在ZF2中,我有一个自定义表单元素工厂。它创建一个自定义MultiCheckbox
并填充数据库查询中的复选框值和标签。
class MyMultiCheckboxFactory
{
public function __invoke(FormElementManager $formElementManager)
{
$multiCheck = new \Zend\Form\Element\MultiCheckbox();
$serviceManager = $formElementManager->getServiceLocator();
$mapper = $serviceManager->get('Path\To\Mapper\To\Query\DB');
$descriptions = $mapper->findDescriptions($id);
// some processing to prepare $value_options array
$multiCheck->setOptions([
'label' => 'blah-blah',
'value_options' => $value_options
]);
return $multiCheck;
}
}
我的问题如下。该方法findDescriptions($id)
取决于$id
我可以从路线上获得的方式。但是当我MyMultiCheckbox
以这种形式使用时:
public function init()
{
$this->add([
'type' => 'Path\To\MyMultiCheckbox',
'name' => 'someName'
]);
}
我不知道该如何将传递$id
给MyMultiCheckbox
。
谁能帮助pleeeeeeeeeease?
您可以通过工厂内部的“路线匹配”实例获取ID 。
$event = $serviceManager->get('Application')->getMvcEvent();
$id = $event->getRouteMatch()->getParam('id', false);
if (empty($id)) {
throw new ServiceNotCreatedException('id not set!');
}
$descriptions = $mapper->findDescriptions($id);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句