diff --git a/module/FormDependencies/Module.php b/module/FormDependencies/Module.php index e04cf4b..b6f54f7 100644 --- a/module/FormDependencies/Module.php +++ b/module/FormDependencies/Module.php @@ -1,6 +1,11 @@ array( - ), - 'factories' => array( + 'invokables' => array(), + 'factories' => array( + + 'formdependencies-model-selecttable' => function ($sm) { + $tableGateway = $sm->get('selecttable-gateway'); + $table = new SelectTable($tableGateway); + + return $table; + }, + 'selecttable-gateway' => function ($sm) { + $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); + $resultSetPrototype = new ResultSet(); + $resultSetPrototype->setArrayObjectPrototype(new SelectOption()); + + return new TableGateway('selectoptions', $dbAdapter, null, $resultSetPrototype); + }, ) ); } diff --git a/module/FormDependencies/config/module.config.php b/module/FormDependencies/config/module.config.php index 624b01b..e2c1686 100644 --- a/module/FormDependencies/config/module.config.php +++ b/module/FormDependencies/config/module.config.php @@ -1,14 +1,16 @@ array( + 'controllers' => array( 'invokables' => array( 'formdependencies-controller-formcontroller' => 'FormDependencies\Controller\FormController' ), ), 'router' => array( 'routes' => array( - 'form-dba-action' => array( - 'type' => 'Zend\Mvc\Router\Http\Literal', + 'form-dba-action' => array( + 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => '/form-dba-action', 'defaults' => array( @@ -17,8 +19,42 @@ ), ), ), + 'form-table-action' => array( + 'type' => 'Zend\Mvc\Router\Http\Literal', + 'options' => array( + 'route' => '/form-table-action', + 'defaults' => array( + 'controller' => 'formdependencies-controller-formcontroller', + 'action' => 'formTable', + ), + ), + ), + 'form-doctrine-action' => array( + 'type' => 'Zend\Mvc\Router\Http\Literal', + 'options' => array( + 'route' => '/form-doctrine-action', + 'defaults' => array( + 'controller' => 'formdependencies-controller-formcontroller', + 'action' => 'formDoctrine', + ), + ), + ), ), ), + 'doctrine' => array( + 'driver' => array( + __NAMESPACE__ . '_driver' => array( + 'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', + 'cache' => 'filesystem', + 'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity') + ), + 'orm_default' => array( + 'drivers' => array( + __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' + ) + ) + ) + ), 'view_manager' => array( 'template_path_stack' => array( __DIR__ . '/../view', diff --git a/module/FormDependencies/src/FormDependencies/Controller/FormController.php b/module/FormDependencies/src/FormDependencies/Controller/FormController.php index a5f2267..4772767 100644 --- a/module/FormDependencies/src/FormDependencies/Controller/FormController.php +++ b/module/FormDependencies/src/FormDependencies/Controller/FormController.php @@ -4,6 +4,8 @@ use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; use FormDependencies\Form\DbAdapterForm; +use FormDependencies\Form\TableForm; +use FormDependencies\Form\DoctrineForm; class FormController extends AbstractActionController { @@ -13,7 +15,33 @@ public function formDbAdapterAction() $vm->setTemplate('form-dependencies/form/form-db-adapter.phtml'); $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); - $form = new DbAdapterForm($dbAdapter); + $form = new DbAdapterForm($dbAdapter); + + return $vm->setVariables(array( + 'form' => $form + )); + } + + public function formTableAction() + { + $vm = new ViewModel(); + $vm->setTemplate('form-dependencies/form/form-table.phtml'); + + $tableGateway = $this->getServiceLocator()->get('formdependencies-model-selecttable'); + $form = new TableForm($tableGateway); + + return $vm->setVariables(array( + 'form' => $form + )); + } + + public function formDoctrineAction() + { + $vm = new ViewModel(); + $vm->setTemplate('form-dependencies/form/form-doctrine.phtml'); + + $entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); + $form = new DoctrineForm($entityManager); return $vm->setVariables(array( 'form' => $form diff --git a/module/FormDependencies/src/FormDependencies/Entity/SelectOption.php b/module/FormDependencies/src/FormDependencies/Entity/SelectOption.php new file mode 100644 index 0000000..7097cde --- /dev/null +++ b/module/FormDependencies/src/FormDependencies/Entity/SelectOption.php @@ -0,0 +1,46 @@ +id = $id; + + return $this; + } + + public function getId() + { + return $this->id; + } + + public function setTitle($title) + { + $this->title = $title; + + return $this; + } + + public function getTitle() + { + return $this->title; + } +} \ No newline at end of file diff --git a/module/FormDependencies/src/FormDependencies/Form/DbAdapterForm.php b/module/FormDependencies/src/FormDependencies/Form/DbAdapterForm.php index 0db413a..346ac42 100644 --- a/module/FormDependencies/src/FormDependencies/Form/DbAdapterForm.php +++ b/module/FormDependencies/src/FormDependencies/Form/DbAdapterForm.php @@ -3,7 +3,6 @@ use Zend\Form\Form; use Zend\Db\Adapter\AdapterInterface; -use Zend\Db\Sql\Sql; class DbAdapterForm extends Form { @@ -19,7 +18,7 @@ public function __construct(AdapterInterface $dbAdapter) 'name' => 'db-select', 'type' => 'Zend\Form\Element\Select', 'options' => array( - 'label' => 'Dynamic DB Select', + 'label' => 'Dynamic DbAdapter Select', 'value_options' => $this->getOptionsForSelect(), 'empty_option' => '--- please choose ---' ) diff --git a/module/FormDependencies/src/FormDependencies/Form/DoctrineForm.php b/module/FormDependencies/src/FormDependencies/Form/DoctrineForm.php new file mode 100644 index 0000000..4f68d40 --- /dev/null +++ b/module/FormDependencies/src/FormDependencies/Form/DoctrineForm.php @@ -0,0 +1,42 @@ +setObjectManager($objectManager); + + parent::__construct('db-adapter-form'); + + $this->add(array( + 'type' => 'DoctrineModule\Form\Element\ObjectSelect', + 'name' => 'name', + 'options' => array( + 'label' => 'Dynamic ObjectManager Select', + 'object_manager' => $this->getObjectManager(), + 'target_class' => 'FormDependencies\Entity\SelectOption', + 'property' => 'title', + 'empty_option' => '--- please choose ---' + ), + )); + } + + public function setObjectManager(ObjectManager $objectManager) + { + $this->objectManager = $objectManager; + + return $this; + } + + public function getObjectManager() + { + return $this->objectManager; + } +} \ No newline at end of file diff --git a/module/FormDependencies/src/FormDependencies/Form/TableForm.php b/module/FormDependencies/src/FormDependencies/Form/TableForm.php new file mode 100644 index 0000000..def08fa --- /dev/null +++ b/module/FormDependencies/src/FormDependencies/Form/TableForm.php @@ -0,0 +1,53 @@ +setSelectTable($selectTable); + + parent::__construct('db-adapter-form'); + + $this->add(array( + 'name' => 'db-select', + 'type' => 'Zend\Form\Element\Select', + 'options' => array( + 'label' => 'Dynamic TableGateway Select', + 'value_options' => $this->getOptionsForSelect(), + 'empty_option' => '--- please choose ---' + ) + )); + } + + public function getOptionsForSelect() + { + $table = $this->getSelectTable(); + $data = $table->fetchAll(); + + $selectData = array(); + + foreach ($data as $selectOption) { + $selectData[$selectOption->id] = $selectOption->title; + } + + return $selectData; + } + + public function setSelectTable($selectTable) + { + $this->selectTable = $selectTable; + + return $this; + } + + public function getSelectTable() + { + return $this->selectTable; + } +} \ No newline at end of file diff --git a/module/FormDependencies/src/FormDependencies/Model/SelectOption.php b/module/FormDependencies/src/FormDependencies/Model/SelectOption.php new file mode 100644 index 0000000..e469d87 --- /dev/null +++ b/module/FormDependencies/src/FormDependencies/Model/SelectOption.php @@ -0,0 +1,22 @@ +id = (isset($data['id'])) ? $data['id'] : null; + $this->title = (isset($data['title'])) ? $data['title'] : null; + } +} \ No newline at end of file diff --git a/module/FormDependencies/src/FormDependencies/Model/SelectTable.php b/module/FormDependencies/src/FormDependencies/Model/SelectTable.php new file mode 100644 index 0000000..35c8b54 --- /dev/null +++ b/module/FormDependencies/src/FormDependencies/Model/SelectTable.php @@ -0,0 +1,54 @@ +tableGateway = $tableGateway; + } + + public function fetchAll() + { + $resultSet = $this->tableGateway->select(); + return $resultSet; + } + + public function getSelectOption($id) + { + $id = (int) $id; + $rowset = $this->tableGateway->select(array('id' => $id)); + $row = $rowset->current(); + if (!$row) { + throw new \Exception("Could not find row $id"); + } + return $row; + } + + public function saveSelectOption(SelectOption $option) + { + $data = array( + 'title' => $option->title, + ); + + $id = (int)$option->id; + if ($id == 0) { + $this->tableGateway->insert($data); + } else { + if ($this->getSelectOption($id)) { + $this->tableGateway->update($data, array('id' => $id)); + } else { + throw new \Exception('Form id does not exist'); + } + } + } + + public function deleteSelectOption($id) + { + $this->tableGateway->delete(array('id' => $id)); + } +} \ No newline at end of file diff --git a/module/FormDependencies/view/form-dependencies/form/form-doctrine.phtml b/module/FormDependencies/view/form-dependencies/form/form-doctrine.phtml new file mode 100644 index 0000000..602dd97 --- /dev/null +++ b/module/FormDependencies/view/form-dependencies/form/form-doctrine.phtml @@ -0,0 +1,13 @@ +headTitle($title);?> + +

+ +form; +$form->setAttribute('action', $this->url('form-table-action')) + ->prepare(); + +echo $this->formCollection($form); +?> \ No newline at end of file diff --git a/module/FormDependencies/view/form-dependencies/form/form-table.phtml b/module/FormDependencies/view/form-dependencies/form/form-table.phtml new file mode 100644 index 0000000..b763899 --- /dev/null +++ b/module/FormDependencies/view/form-dependencies/form/form-table.phtml @@ -0,0 +1,13 @@ +headTitle($title);?> + +

+ +form; +$form->setAttribute('action', $this->url('form-table-action')) + ->prepare(); + +echo $this->formCollection($form); +?> \ No newline at end of file