Skip to content

Instantly share code, notes, and snippets.

@rufinus
Created November 12, 2012 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rufinus/4059514 to your computer and use it in GitHub Desktop.
Save rufinus/4059514 to your computer and use it in GitHub Desktop.
Where is my Service Locator gone?
<?php
namespace Admin\Form;
use Zend\Form\Element\Select;
class Agency extends Form {
public function __construct()
{
parent::__construct('agency');
$this->setAttribute('method', 'post');
$this->addHidden('agency_id');
$this->addHidden('mandator_id', 1);
$this->addText('name', 'Name', true);
$this->addText('address', 'Address');
$this->addText('address2', 'Address2');
$this->addText('zipcode', 'Zipcode');
$this->addText('city', 'City');
$this->addText('uid', 'UID');
$select = new Select('country');
$select->setAttribute('type', 'select')
->setValueOptions($this->countryOptions())
->setLabel('Country');
$this->add($select);
$address = new DisplayGroup('fs_address', 'Address');
$address->add('name')
->add('address')
->add('address2')
->add('zipcode')
->add('city')
->add('country')
->add('uid');
$this->addDisplayGroup($address);
$this->addText('phone', 'Phone');
$this->addText('fax', 'Fax');
$this->addText('url', 'URL');
$this->addText('email', 'EMail');
$contact = new DisplayGroup('fs_contact', 'Contact');
$contact->add('phone')
->add('fax')
->add('url')
->add('email');
$this->addDisplayGroup($contact);
$select = new Select('mandator_id');
$select->setAttribute('type', 'select')
->setValueOptions($this->getMandator())
->setLabel('Mandator');
$this->add($select);
$admin = new DisplayGroup('fs_admin', 'Administration');
$admin->add('mandator_id');
$this->addDisplayGroup($admin);
}
public function getMandator() {
var_dump($this->getServiceLocator()); // <--- NULL !?!?
return array();
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$rep = $em->getRepository($this->table);
$rows = $rep->fetchAll();
$data = array();
foreach($rows as $row) {
$data[$row->mandator_id] = $row->name;
}
return $data;
}
}
public function createAction()
{
$service = $this->getService();
$form = $service->getForm();
var_dump($form->getServiceLocator()); // as expected service locator object
$entity = $service->getEntity();
$form->bind($entity);
if($this->request->isPost()) {
$form->setData($this->request->getPost());
$this->getLogger()->info('Form Post', $_POST);
if($form->isValid()) {
$service->persist($entity);
$this->getLogger()->info($entity->getLangkey().' Successfully saved');
$this->getMessenger()->setNamespace('ok')->addMessage('%s successfully saved');
$this->redirect()->toUrl('/agency/list');
}
}
return new ViewModel(array('form' => $form));
}
/**
* Get Service Class
* @return \Admin\Service\I18n
*/
protected function getService()
{
return $this->getServiceLocator()->get('Service_Agency');
}
namespace cwdAdmin\Service;
use cwdCommon\Service;
/**
* ServiceFactory
*
* @category cwdAdmin
* @package CWD
* @subpackage Service
**/
class ServiceFactory implements ServiceLocatorAwareInterface
{
/**
* @var \Zend\ServiceManager\ServiceLocatorInterface
*/
protected $serviceLocator;
public function setUp(Service $object)
{
$object->setServiceLocator($this->getServiceLocator());
$object->setLogger($this->getServiceLocator()->get('Logger'));
$form = $object->getFormClass();
$entity = $object->getEntityClass();
$form->setServiceLocator($this->getServiceLocator());
if($form instanceof InputFilterAwareInterface) {
$form->setInputFilter($this->getEntity()->getInputFilter());
}
$form->changeViewHelper();
$form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());
$object->setEntity($entity);
$object->setForm($form);
return $object;
}
/* (non-PHPdoc)
* @see \Zend\ServiceManager\ServiceLocatorAwareInterface::getServiceLocator()
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}
/* (non-PHPdoc)
* @see \Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator()
*/
public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this;
}
}
'invokables' => array(
'serviceFactory' => 'cwdAdmin\Service\ServiceFactory',
),
'factories' => array(
'Service_Agency' => function ($sm) {
$factory = $sm->get('serviceFactory');
$object = $factory->setUp(new \Admin\Service\Agency());
return $object;
}
)
@rufinus
Copy link
Author

rufinus commented Nov 12, 2012

if i call $form->getMandator() from the controller it workds... called inside, no serviceLocator....

@rufinus
Copy link
Author

rufinus commented Nov 12, 2012

DONT setup your form in the __constructor if you inject it with dependencies.... really just dont :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment