Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple Zend Framework model class, used to build a set of unit tests with PHPUnit and Mockery.
<?php
namespace Generic\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
class GenericTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getGeneric($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
return ($rowset->count() !== 0) ? $rowset->current() : false;
}
public function fetchSelect()
{
$sqlSelect = $this->tableGateway->getSql()->select();
$sqlSelect->where(array(
"firstName" => "Michael",
"lastName" => "Michael"
))->order("lastName DESC");
return $this->tableGateway->select($sqlSelect);
}
public function fetchComplexSelect()
{
$sqlSelect = $this->tableGateway->getSql()->select();
$sqlSelect->where(array(
"firstName" => "Matthew",
"lastName" => "Setter"
))/*->where
->nest()
->like("email", "%gmail.com")
// ->in("age", array(4, 10, 20))
// ->between("income", 20000, 30000)
->unnest()*/;
$sqlSelect->order("lastName DESC");
return $this->tableGateway->select($sqlSelect);
}
public function findByName($firstName = null, $lastName = null, $orderBy = null)
{
$sqlSelect = $this->tableGateway->getSql()->select();
if (!empty($firstName) && !empty($lastName)) {
$sqlSelect->where(array(
"firstName" => $firstName,
"lastName" => $lastName
));
} else {
if (!empty($firstName)) {
$sqlSelect->where(array(
"firstName" => $firstName
));
}
if (!empty($lastName)) {
$sqlSelect->where(array(
"lastName" => $lastName
));
}
}
if (empty($orderBy)) {
$sqlSelect->order("lastName DESC");
} else {
$sqlSelect->order($orderBy);
}
return $this->tableGateway->select($sqlSelect);
}
public function fetchSelectWithClosure()
{
return $this->tableGateway->select(function(Select $select) {
return $select->where(array("age" => 20));
});
}
public function fetchReturnRecordsWithAgeOverTwenty($age)
{
$sqlSelect = $this->tableGateway->getSql()->select();
$sqlSelect->where(array("age" => $age));
return $this->tableGateway->select($sqlSelect);
}
public function fetchAllByFirstName($firstName)
{
if (empty($firstName)) {
return false;
}
$sqlSelect = $this->tableGateway->getSql()->select();
$sqlSelect->where(array("firstName" => $firstName));
return $this->tableGateway->select($sqlSelect);
}
public function saveGeneric(Generic $Generic)
{
$data = array(
'artist' => $Generic->artist,
'title' => $Generic->title,
);
$id = (int)$Generic->id;
if ($id == 0) {
return $this->tableGateway->insert($data);
} else {
if ($this->getGeneric($id)) {
return $this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Form id does not exist');
}
}
}
public function deleteGeneric($id)
{
if (empty($id)) {
return false;
}
return $this->tableGateway->delete(array('id' => $id));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.