Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace DrinkManagement\Model;
- use Zend\Db\TableGateway\AbstractTableGateway,
- Zend\Db\Adapter\Adapter,
- Zend\Db\ResultSet\ResultSet,
- Zend\Db\Sql\Select;
- class DrinkTable extends AbstractTableGateway
- {
- protected $table ='drink';
- protected $tableName ='drink';
- public function __construct(Adapter $adapter)
- {
- $this->adapter = $adapter;
- $this->resultSetPrototype = new ResultSet(new Drink);
- $this->initialize();
- }
- public function fetchAll()
- {
- $resultSet = $this->select();
- return $resultSet;
- }
- public function newSelect() {
- return new Select;
- }
- public function getSelect(&$select,$columnsArray=array())
- {
- $select = new Select;
- return $select->from('drink')->columns($columnsArray);
- }
- public function createIfNotExist($checkColumnsArray,$optionalColumns=array(),&$isRowCreated=null) {
- $rowset=$this->select($checkColumnsArray);
- $row = $rowset->current();
- $id=null;
- if ($row == null) {
- $allColumns=array_merge($checkColumnsArray,$optionalColumns);
- $affectedRows = $this->insert($allColumns);
- if ($affectedRows != 1) {
- throw new \Exception("error: could not add line to db");
- }
- $id=$this->lastInsertValue;
- $isRowCreated=true;
- } else {
- $id=$row->drink_id;
- $isRowCreated=false;
- }
- return $id;
- }
- //http://stackoverflow.com/questions/6156942/how-do-i-insert-an-empty-row-but-have-the-autonumber-update-correctly
- public function createEmptyRow() {
- $row=array(
- 'drink_id' => null
- );
- $affectedRows=$this->insert($row);
- if ($affectedRows != 1) {
- throw new \Exception("error: could not add empty row to db");
- }
- $id=$this->lastInsertValue;
- return $id;
- }
- public function getDrink($id)
- {
- $id = (int) $id;
- $rowset = $this->select(array('drink_id' => $id));
- $row = $rowset->current();
- if (!$row) {
- throw new \Exception("Could not find row $id");
- }
- return $row;
- }
- public function saveDrink(Drink $drink)
- {
- $data = array(
- 'drink_type_id' => $drink->drink_type_id,
- 'drink_brand_id' => $drink->drink_brand_id,
- 'creation_timestamp' => $drink->creation_timestamp,
- );
- $id = (int)$drink->id;
- if ($id == 0) {
- $this->insert($data);
- } else {
- if ($this->getDrink($id)) {
- $this->update($data, array('drink_id' => $id));
- } else {
- throw new \Exception('Form id does not exit');
- }
- }
- }
- public function addDrink($drink_type_id, $drink_brand_id = null, $creation_timestamp = null)
- {
- $data = array( 'drink_type_id' => $drink_type_id,
- 'drink_brand_id' => $drink_brand_id,
- 'creation_timestamp' => $creation_timestamp,
- );
- $affectedRows=$this->insert($data);
- if ($affectedRows != 1) {
- return null;
- }
- return $this->lastInsertValue;
- }
- public function updateDrink($drink_id, $drink_type_id, $drink_brand_id, $creation_timestamp)
- {
- $data = array(
- 'drink_type_id' => $drink->drink_type_id,
- 'drink_brand_id' => $drink->drink_brand_id,
- 'creation_timestamp' => $drink->creation_timestamp,
- );
- $this->update($data, array(drink_id => $id));
- }
- public function deleteDrink($id)
- {
- $this->delete(array('drink_id' => $id));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement