Select ZF2(2.1方法)填充表单中的选择/下拉元素
我使用Zend Framework 2.1(不是2.0x)方法来填充选择/下拉列表,该列表在以下链接中描述:Select ZF2(2.1方法)填充表单中的选择/下拉元素,select,drop-down-menu,zend-framework2,Select,Drop Down Menu,Zend Framework2,我使用Zend Framework 2.1(不是2.0x)方法来填充选择/下拉列表,该列表在以下链接中描述: 虽然我似乎按照他们说的做了,但我收到了一条错误消息,如: *…::__construct()必须是Zend\Db\TableGateway\TableGateway的实例,未给定,在* 这似乎是服务定位器没有正确使用 添加我的字段集的我的表单代码SupplierFieldset: namespace Inventory\Form; use Zend\Form\Form; use
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;
}
}
My 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;
}
)
);
}
My 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
参数。但是,当从供应商控制器
调用时,此模型工作正常