Friday, 15 July 2011

zend framework2 - How To Create Custom Form Elements That Extend Doctrine Entity Select Elements -



zend framework2 - How To Create Custom Form Elements That Extend Doctrine Entity Select Elements -

i trying create custom element can pre-set entity namespace -- application\entity\user , add together element form. have issue injecting doctrine em in form elements -- found post form_elements key in configuration doesnt work -- figured maybe setup element , pass objectmanager form.

i want this.

$this->add( array( 'name' => 'user_id', 'type' => 'application\form\element\userselect', 'options' => array( 'label' => 'user', 'object_manager' => $this->getobjectmanager(), ), ), );

or improve this

$this->add( array( 'name' => 'user_id', 'type' => 'application\form\element\userselect', 'label' => 'user', ), );

module.config.php

'service_manager' => array( 'aliases' => array( 'doctrine_service' => 'doctrine.entitymanager.orm_default', ), 'initializers' => array( function ($instance, $sm) { if ($instance instanceof doctrinemodule\persistence\objectmanagerawareinterface) { $instance->setobjectmanager( $sm->get('doctrine_service') ); } if ($instance instanceof application\form\abstractform) { $instance->init(); } }, ), ),

abstractform.php

namespace application\form; utilize doctrine\common\persistence\objectmanager; utilize doctrinemodule\persistence\objectmanagerawareinterface; utilize zend\form\form zendform; abstract class abstractform extends zendform implements objectmanagerawareinterface { protected $objectmanager; public function setobjectmanager(objectmanager $objectmanager) { $this->objectmanager = $objectmanager; } public function getobjectmanager() { homecoming $this->objectmanager; } }

testform.php

namespace users\form; utilize application\form\abstractform; class login extends abstractform { public function init() { $this->add( array( 'name' => 'user_id', 'type' => 'doctrineormmodule\form\element\doctrineentity', 'options' => array( 'label' => 'user', 'object_manager' => $this->getobjectmanager(), 'target_class' => 'application\entity\user', 'property' => 'name', 'find_method' => array( 'name' => 'findby', 'params' => array( 'criteria' => array('is_deleted' => 0), 'orderby' => array('name' => 'asc'), ), ), ), ), ); } }

add initializer , invokables getformelementconfig method of module.php:

use doctrinemodule\persistence\objectmanagerawareinterface; ... public function getformelementconfig() { homecoming array( 'invokables' => array( 'myform' => 'application\form\myform', ), 'initializers' => array( 'objectmanagerinitializer' => function ($element, $formelements) { if ($element instanceof objectmanagerawareinterface) { $services = $formelements->getservicelocator(); $entitymanager = $services->get('doctrine\orm\entitymanager'); $element->setobjectmanager($entitymanager); } }, ), ); }

then utilize formelementmanager te form:

$forms = $this->getservicelocator()->get('formelementmanager'); $myform = $forms->get('myform');

finally add together element within init method - not constructor because never aware of objectmanager:

public function init() { $this->add( array( 'name' => 'user_id', 'type' => 'doctrineormmodule\form\element\doctrineentity', 'options' => array( 'label' => 'user', 'object_manager' => $this->getobjectmanager(), 'target_class' => 'application\entity\user', 'property' => 'name', 'find_method' => array( 'name' => 'findby', 'params' => array( 'criteria' => array('is_deleted' => 0), 'orderby' => array('name' => 'asc'), ), ), ), ), ); }

see give-and-take on setup here.

zend-framework2

No comments:

Post a Comment