Zend FrameWork 2 Get ServiceLocator In Form and populate a drop down list

前端 未结 6 1354
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 14:40

I need to get the adapter from the form, but still could not.

In my controller I can recover the adapter using the following:



        
6条回答
  •  南笙
    南笙 (楼主)
    2021-02-02 15:10

    In module.php I create two services. See how I feed the adapter to the form.

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'db_adapter' =>  function($sm) {
                    $config = $sm->get('Configuration');
                    $dbAdapter = new \Zend\Db\Adapter\Adapter($config['db']);
                    return $dbAdapter;
                },
    
                'my_amazing_form' => function ($sm) {
                    return new \dir\Form\SomeForm($sm->get('db_adapter'));
                },
    
            ),
        );
    }
    

    In the form code I use that feed to whatever:

    namespace ....\Form;
    
    use Zend\Form\Factory as FormFactory;
    use Zend\Form\Form;
    
    class SomeForm extends Form
    {
    
        public function __construct($adapter, $name = null)
        {
            parent::__construct($name);
            $factory = new FormFactory();
    
            if (null === $name) {
                $this->setName('whatever');
            }
    
        }
    }
    

提交回复
热议问题