ZF2(2.1方法)在表单中填充选择/下拉元素

时间:2013-02-03 01:36:02

标签: select drop-down-menu zend-framework2

我使用Zend Framework 2.1(而非2.0x)方法填充以下链接中描述的选择/下拉列表:

虽然看起来我已经做了,因为他们告诉我收到的错误信息如下:

  

* ... :: __ construct()必须是Zend \ Db \ TableGateway \ TableGateway的一个实例,没有给出,在...中调用...

似乎没有正确使用服务定位器。

我的表单代码添加了我的FieldSet SupplierFieldset

namespace Inventory\Form;
use Zend\Form\Form;
use Inventory\Model;

class ItemForm extends Form
{
    public function init()
    {
        $this->add(array(
                'name' => 'sup_code',
                'type' => 'Inventory\Form\SupplierFieldset'
        ));
    }
}

我的'SupplierFieldset'类:

namespace Inventory\Form;

use Inventory\Model;
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\InputFilter\InputFilterProviderInterface;

use Inventory\Model\SupplierTable;
use Inventory\Model\Supplier;

class SupplierFieldset extends Fieldset implements ServiceLocatorAwareInterface
{
    protected $serviceLocator;
    protected $supplierTable;

    public function init()
    {
        parent::__construct('Suppliers Code');
        $this->setLabel('Supplier Code');
        $this->setName('supplier_code');
        $suppliers = $this->getSupplierTable()->fetchAll();

       $select = new Element\Select('supplier_code');
        $options = array();
        foreach ($suppliers as $supplier) {
            $options[$supplier->id] = $supplier->sup_code;
        }
        $select->setValueOptions($options);
    }

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

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

    public function getSupplierTable()
    {
        if (!$this->supplierTable) {
            $sm = $this->getServiceLocator();
            $this->supplierTable = $sm->get('Inventory\Model\SupplierTable');
        }
        return $this->supplierTable;
    }
}

我的Module.php getFormElementConfig()函数:

public function getFormElementConfig()
    {
        return array(
            'factories' => array(
                'SupplierFieldset' => function($sm) {
                    $serviceLocator = $sm->getServiceLocator();
                    $supplierTable = $serviceLocator->get('Inventory\Model\SupplierTable');
                    $fieldset = new SupplierFieldset($supplierTable);
                    return $fieldset;
                 }
            )
        );
    }

我的SupplierTable.php模型:

namespace Inventory\Model;

use Zend\Db\TableGateway\TableGateway;

class SupplierTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll()
    {
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }
}

我知道SupplierTable模型的构造函数需要TableGateway $tableGateway参数。但是当从SupplierController调用时,此模型正常工作。

0 个答案:

没有答案