ho bisogno di ottenere l'adattatore dalla forma verso il basso, ma ancora non poteva.Zend Framework 2 Get ServiceLocator nella forma e compilare un elenco a discesa
Nel mio controller posso recuperare l'adattatore utilizzando il seguente:
// 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;
}
Nel mio modulo ho fatto così:
// 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;
},
),
);
}
Qualcuno potrebbe darmi un esempio di come raggiungere l'adattatore al tabella dal modulo di gruppo?
Ho seguito questo esempio per i miei utenti di forma: http://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html
CURA da qui ...
Forse mi sono espresso male per fare la domanda.
Quello che devo veramente fare è compilare una selezione (Discesa verso il basso) con le informazioni dai miei gruppi di tabelle.
Così ho bisogno di ottenere i servizi all'interno della mia classe form da ServiceLocatorAwareInterface (see this link) implementata perché per impostazione predefinita, la Zend Framework MVC registra un inizializzatore che inietterà nel ServiceManager istanza ServiceLocatorAwareInterface implementazione di qualsiasi classe.
Dopo aver recuperato i valori dai gruppi di tabelle e compilato la selezione.
Il problema è che di tutti i modi che ho provato, il getServiceLocator() restituisce questo:
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
Volevo solo fare questo nel mio form ...
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...
hi hai trovato la soluzione per questo? –