我需要從窗體中獲取適配器,但仍不能。Zend FrameWork 2在窗體中獲取ServiceLocator並填充下拉列表
在我的控制,我可以使用恢復適配器如下:
// 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
來自此編輯...
也許是我表達自己錯了要問的問題。
我真正需要做的是用我的表組中的信息填充select(Drop Down)。
所以我需要通過ServiceLocatorAwareInterface(see this link)實現,因爲默認情況下,讓我的用戶窗體類中的服務,Zend Framework的MVC註冊一個初始化這將注入的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...
你好你是否找到了解決方案? –