-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Zend\Form\Element\Select using Zend\Db\TableGateway and DoctrineModul…
…e ObjectSelect
- Loading branch information
Manuel Stosic
committed
Mar 22, 2013
1 parent
747d912
commit 45a5dce
Showing
11 changed files
with
333 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
module/FormDependencies/src/FormDependencies/Entity/SelectOption.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
module/FormDependencies/src/FormDependencies/Form/DoctrineForm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
53
module/FormDependencies/src/FormDependencies/Form/TableForm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
module/FormDependencies/src/FormDependencies/Model/SelectOption.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
module/FormDependencies/src/FormDependencies/Model/SelectTable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.