フォームからアダプタを取得する必要がありますが、それでも解決できません。私のコントローラでZend FrameWork 2 Form内で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
...
は、たぶん私は質問をするために自分が間違った表現。
私が本当に必要とするのは、選択(ドロップダウン)にテーブルグループの情報を入力することです。
ServiceLocatorAwareInterface(see this link)でuserFormクラス内のサービスを取得する必要があります。デフォルトでは、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...
こんにちは、あなたはこれの解決策を見つけましたか? –