I'm writing a PHP application that is not based on zf2 mvc.
I do want to use only the Zend_Db zf2 module. how can I configure my application to know how to find the Zend_Db releveant PHP file where required ?
I have zf2 Zend_db module download with phyrus and installed at the location vendor/zf2/php
.
I tried adding the module to the include path with the following command:
set_include_path("../vendor/zf2/php".PATH_SEPARATOR.get_include_path());
I created Model class files relevant to each table (using zend-db-model-generator) inside directory Model/
.
my main app contains the following:
use DrinkManagement\Model\DrinkTable;
use Zend\Db\Adapter\Adapter;
set_include_path("../vendor/zf2/php".PATH_SEPARATOR.get_include_path());
require_once('Model/DrinkTable.php');
/**
@var DrinkManagement\Model\Drink
*/
$drinkTable=null;
$drinkTable = new DrinkTable();
$res=$drinkTable->getDrink(1);
echo var_export($res,1);
my DrinkTable class:
namespace DrinkManagement\Model;
use Zend\Db\TableGateway\AbstractTableGateway,
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));
}
}
when I try to execute my main php application i get the following error message:
PHP Fatal error: Class 'Zend\Db\TableGateway\AbstractTableGateway' not found in /Users/ufk/Documents/workspace/tux-drink/TuxDb/mysql/Model/DrinkTable.php on line 10
any ideas how to resolve the issue without adding require_once everywhere ?
maybe is there another zf2 component I can use that will autoload the relevant classes?