zend-framework2 - Zend FrameWork 2 Get ServiceLocator In Form 并填充下拉列表

标签 zend-framework2

我需要从表单中获取适配器,但仍然无法。

在我的 Controller 中,我可以使用以下方法恢复适配器:

// module/Users/src/Users/Controller/UsersController.php
public function getUsersTable ()
{
    if (! $this->usersTable) {
        $sm = $this->getServiceLocator();
        $this->usersTable = $sm->get('Users\Model\UsersTable');
    }
    return $this->usersTable;
}

在我的模块中,我这样做了:

// module/Users/Module.php  
public function getServiceConfig()
{
    return array(
            'factories' => array(
                    'Users\Model\UsersTable' =>  function($sm) {
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $uTable     = new UsersTable($dbAdapter);
                        return $uTable;
                    },
                    //I need to get this to the list of groups
                    'Users\Model\GroupsTable' =>  function($sm) {
                        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                        $gTable     = new GroupsTable($dbAdapter);
                        return $gTable;
                    },
            ),
    );
}

有人可以给我一个例子,如何将适配器从组表单中获取到表中?

我已将此示例应用于我的表单用户:
http://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html

从这里编辑...

也许我表达了自己错误地提出这个问题。

我真正需要做的是用我的表组中的信息填充一个选择(下拉)。

所以我需要通过 ServiceLocatorAwareInterface (see this link) 实现我的 userForm 类中的服务,因为默认情况下,Zend Framework MVC 注册了一个初始化器,它将注入(inject)到 ServiceManager 实例 ServiceLocatorAwareInterface 实现任何类。

从表组中检索值并填充选择后。

问题是在我尝试过的所有方法中,getServiceLocator() 返回:
Call to a member function get() on a non-object in
D:\WEBSERVER\htdocs\Zend2Control\module\Users\src\Users\Form\UsersForm.php
on line 46

我只是想在我的用户窗体中这样做......

namespace Users\Form;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Form\Element;
use Zend\Form\Form;

class UsersForm extends Form implements ServiceLocatorAwareInterface
{

    protected $serviceLocator;

    public function getServiceLocator ()
    {
        return $this->serviceLocator;
    }

    public function setServiceLocator (ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    public function __construct ($name = null)
    {
        parent::__construct('users');

        $this->setAttribute('method', 'post');        

        $sm = $this->getServiceLocator();

        $groups = $sm->get('Users\Model\GroupsTable')->fetchAll(); // line 46       

        $select = new Element\Select('groups');

        $options = array();

        foreach ($groups as $group) {

            $options[$group->id] = $group->name;
        }

        $select->setValueOptions($options);

        $this->add($select);

        // and more elements here...

最佳答案

对于 ZF < 2.1,此处的其他各种答案通常是正确的。

一旦 2.1 出来,框架就有一个漂亮的 nice solution .这或多或少形式化了 DrBeza 的解决方案,即:使用初始化程序,然后将任何表单引导移动到在所有依赖项都已初始化后调用的 init() 方法中。

我一直在玩开发分支,它工作得很好。

关于zend-framework2 - Zend FrameWork 2 Get ServiceLocator In Form 并填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12459180/

相关文章:

php - 从多对多表中返回缺失的结果

php - 准备 Zend Framework 2(命名空间 Controller )

cookies - Zf2 - 如何设置 cookie

select - 如何扩展 Zend\Db\Sql\Select?

javascript - 模态布局放在哪里以及如何用动态内容填充它

zend-framework2 - 如何为表单生成新的 Csrf-Token

php - Zend 框架 2 : Catch all route and url escape

forms - zf2 表单验证器 DateTime dateInvalidDate

php - zend 框架 2 错误报告

php - 使用 Zend Framework 2 加速 6,000 行查询