Advertisement
kfirufk

DrinkTable class

Nov 28th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.84 KB | None | 0 0
  1. <?php
  2. namespace DrinkManagement\Model;
  3.  
  4. use Zend\Db\TableGateway\AbstractTableGateway,
  5.     Zend\Db\Adapter\Adapter,
  6.     Zend\Db\ResultSet\ResultSet,
  7.     Zend\Db\Sql\Select;
  8.  
  9. class DrinkTable extends AbstractTableGateway
  10. {
  11.     protected $table ='drink';
  12.     protected $tableName ='drink';
  13.  
  14.     public function __construct(Adapter $adapter)
  15.     {
  16.         $this->adapter = $adapter;
  17.         $this->resultSetPrototype = new ResultSet(new Drink);
  18.  
  19.         $this->initialize();
  20.     }
  21.  
  22.     public function fetchAll()
  23.     {
  24.         $resultSet = $this->select();
  25.         return $resultSet;
  26.     }
  27.    
  28.     public function newSelect() {
  29.         return new Select;
  30.     }
  31.    
  32.     public function getSelect(&$select,$columnsArray=array())
  33.     {
  34.         $select = new Select;
  35.         return $select->from('drink')->columns($columnsArray);     
  36.     }
  37.    
  38.     public function createIfNotExist($checkColumnsArray,$optionalColumns=array(),&$isRowCreated=null) {
  39.             $rowset=$this->select($checkColumnsArray);
  40.             $row = $rowset->current();
  41.             $id=null;
  42.             if ($row == null) {
  43.                 $allColumns=array_merge($checkColumnsArray,$optionalColumns);
  44.                 $affectedRows = $this->insert($allColumns);
  45.                 if ($affectedRows != 1) {
  46.                     throw new \Exception("error: could not add line to db");
  47.                 }
  48.                 $id=$this->lastInsertValue;
  49.                 $isRowCreated=true;
  50.             } else {
  51.                 $id=$row->drink_id;
  52.                 $isRowCreated=false;
  53.             }
  54.             return $id;
  55.     }
  56.    
  57.     //http://stackoverflow.com/questions/6156942/how-do-i-insert-an-empty-row-but-have-the-autonumber-update-correctly
  58.    
  59.     public function createEmptyRow() {
  60.         $row=array(
  61.         'drink_id' => null
  62.         );
  63.         $affectedRows=$this->insert($row);
  64.         if ($affectedRows != 1) {
  65.             throw new \Exception("error: could not add empty row to db");
  66.         }
  67.         $id=$this->lastInsertValue;
  68.         return $id;
  69.     }
  70.    
  71.     public function getDrink($id)
  72.     {
  73.         $id  = (int) $id;
  74.         $rowset = $this->select(array('drink_id' => $id));
  75.         $row = $rowset->current();
  76.         if (!$row) {
  77.             throw new \Exception("Could not find row $id");
  78.         }
  79.         return $row;
  80.     }
  81.  
  82.     public function saveDrink(Drink $drink)
  83.     {
  84.         $data = array(
  85.                         'drink_type_id' => $drink->drink_type_id,
  86.                         'drink_brand_id' => $drink->drink_brand_id,
  87.                         'creation_timestamp' => $drink->creation_timestamp,
  88.                     );
  89.  
  90.         $id = (int)$drink->id;
  91.         if ($id == 0) {
  92.             $this->insert($data);
  93.         } else {
  94.             if ($this->getDrink($id)) {
  95.                 $this->update($data, array('drink_id' => $id));
  96.             } else {
  97.                 throw new \Exception('Form id does not exit');
  98.             }
  99.         }
  100.     }
  101.  
  102.     public function addDrink($drink_type_id, $drink_brand_id = null, $creation_timestamp = null)
  103.     {
  104.         $data = array(            'drink_type_id' => $drink_type_id,
  105.                         'drink_brand_id' => $drink_brand_id,
  106.                         'creation_timestamp' => $creation_timestamp,
  107.                     );
  108.         $affectedRows=$this->insert($data);
  109.                 if ($affectedRows != 1) {
  110.             return null;
  111.         }
  112.         return $this->lastInsertValue;
  113.             }
  114.  
  115.     public function updateDrink($drink_id, $drink_type_id, $drink_brand_id, $creation_timestamp)
  116.     {
  117.         $data = array(
  118.                         'drink_type_id' => $drink->drink_type_id,
  119.                         'drink_brand_id' => $drink->drink_brand_id,
  120.                         'creation_timestamp' => $drink->creation_timestamp,
  121.                             );
  122.         $this->update($data, array(drink_id => $id));
  123.     }
  124.  
  125.     public function deleteDrink($id)
  126.     {
  127.         $this->delete(array('drink_id' => $id));
  128.     }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement