Skip to content

Commit

Permalink
Zend\Form\Element\Select using Zend\Db\TableGateway and DoctrineModul…
Browse files Browse the repository at this point in the history
…e ObjectSelect
  • Loading branch information
Manuel Stosic committed Mar 22, 2013
1 parent 747d912 commit 45a5dce
Show file tree
Hide file tree
Showing 11 changed files with 333 additions and 9 deletions.
24 changes: 21 additions & 3 deletions module/FormDependencies/Module.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php
namespace FormDependencies;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use FormDependencies\Model\SelectTable;
use FormDependencies\Model\SelectOption;

class Module
{
public function getConfig()
Expand All @@ -11,9 +16,22 @@ public function getConfig()
public function getServiceConfig()
{
return array(
'invokables' => 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);
},
)
);
}
Expand Down
42 changes: 39 additions & 3 deletions module/FormDependencies/config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php
namespace FormDependencies;

return array(
'controllers' => 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(
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace FormDependencies\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="selectoptions")
*/
class SelectOption
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
*/
protected $id;

/**
* @ORM\Column(name="title", type="string")
*/
protected $title;

public function setId($id)
{
$this->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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Sql\Sql;

class DbAdapterForm extends Form
{
Expand All @@ -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 ---'
)
Expand Down
42 changes: 42 additions & 0 deletions module/FormDependencies/src/FormDependencies/Form/DoctrineForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace FormDependencies\Form;

use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\Form\Form;

class DoctrineForm extends Form implements ObjectManagerAwareInterface
{
protected $objectManager;

public function __construct(ObjectManager $objectManager)
{
$this->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;
}
}
53 changes: 53 additions & 0 deletions module/FormDependencies/src/FormDependencies/Form/TableForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace FormDependencies\Form;

use Zend\Form\Form;
use FormDependencies\Model\SelectTable;

class TableForm extends Form
{
protected $selectTable;

public function __construct(SelectTable $selectTable)
{
$this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: du009701
* Date: 22.03.13
* Time: 11:18
* To change this template use File | Settings | File Templates.
*/

namespace FormDependencies\Model;

class SelectOption
{
public $id;
public $title;

public function exchangeArray($data)
{
$this->id = (isset($data['id'])) ? $data['id'] : null;
$this->title = (isset($data['title'])) ? $data['title'] : null;
}
}
54 changes: 54 additions & 0 deletions module/FormDependencies/src/FormDependencies/Model/SelectTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace FormDependencies\Model;

use Zend\Db\TableGateway\TableGateway;

class SelectTable
{
protected $tableGateway;

public function __construct(TableGateway $tableGateway)
{
$this->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));
}
}
Loading

0 comments on commit 45a5dce

Please sign in to comment.