我需要从表单中获取适配器,但仍然无法获取。
在我的控制器中,我可以使用以下命令恢复适配器:
// 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)实现userForm类中的服务,因为在默认情况下,Zend Framework MVC注册了一个初始化器,该初始化器将注入到实现任何类的ServiceManager实例ServiceLocatorAwareInterface中。
从表组中检索值并填充select之后。
问题是,在我尝试过的所有方法中,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
我只想在我的UserForm里做这个。
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...
发布于 2013-01-17 08:01:08
对于ZF < 2.1,这里的其他各种答案通常是正确的。
一旦2.1发布,这个框架就有了很好的nice solution。这或多或少地形式化了DrBeza的解决方案,即:使用初始化器,然后将任何form-bootstrapping移动到init()方法中,该方法在初始化所有依赖项之后调用。
我一直在尝试开发分支,如果它工作得很好的话。
发布于 2012-11-28 20:35:20
这是我用来解决这个问题的方法。
首先,您想让您的表单像您所做的那样实现ServiceLocatorInterface。
然后你仍然需要手动注入服务定位器,当整个表单在构造器中生成时,你也需要通过构造器注入(虽然在构造器中构建它并不理想)。
Module.php
/**
* Get the service Config
*
* @return array
*/
public function getServiceConfig()
{
return array(
'factories' => array(
/**
* Inject ServiceLocator into our Form
*/
'MyModule\Form\MyForm' => function($sm) {
$form = new \MyModule\Form\MyFormForm('formname', $sm);
//$form->setServiceLocator($sm);
// Alternativly you can inject the adapter/gateway directly
// just add a setter on your form object...
//$form->setAdapter($sm->get('Users\Model\GroupsTable'));
return $form;
},
),
);
}
现在,在您的控制器中,您将获得如下形式的表单:
// Service locator now injected
$form = $this->getServiceLocator()->get('MyModule\Form\MyForm');
现在,您将可以访问表单中的完整服务定位器,以获取任何其他服务等,例如:
$groups = $this->getServiceLocator()->get('Users\Model\GroupsTable')->fetchAll();
发布于 2012-09-18 20:51:48
在module.php中,我创建了两个服务。看看我是如何将适配器提供给表单的。
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'));
},
),
);
}
在表单代码中,我使用该提要来做任何事情:
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');
}
}
}
https://stackoverflow.com/questions/12459180
复制相似问题