Skip to content

Instantly share code, notes, and snippets.

@nclundsten
Created June 18, 2014 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nclundsten/d537a911ff4d7765d45b to your computer and use it in GitHub Desktop.
Save nclundsten/d537a911ff4d7765d45b to your computer and use it in GitHub Desktop.
<?php
namespace Pledgevine\Db;
use Zend\Db\Sql\Select;
use Zend\Db\TableGateway\AbstractTableGateway;
//works exactly like a table gateway except it wont throw exceptions if you havent specified a table
//notice the lines are just commented
//
//heres a use case: you want to select from a select (subquery/subselect) instead of a table
class DbGateway extends AbstractTableGateway
{
protected function executeSelect(Select $select)
{
$selectState = $select->getRawState();
//if ($selectState['table'] != $this->table) {
// throw new Exception\RuntimeException('The table name of the provided select object must match that of the table');
//}
if ($selectState['columns'] == array(Select::SQL_STAR)
&& $this->columns !== array()) {
$select->columns($this->columns);
}
// apply preSelect features
$this->featureSet->apply('preSelect', array($select));
// prepare and execute
$statement = $this->sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
// build result set
$resultSet = clone $this->resultSetPrototype;
$resultSet->initialize($result);
// apply postSelect features
$this->featureSet->apply('postSelect', array($statement, $result, $resultSet));
return $resultSet;
}
protected function executeInsert(Insert $insert)
{
$insertState = $insert->getRawState();
//if ($insertState['table'] != $this->table) {
// throw new Exception\RuntimeException('The table name of the provided Insert object must match that of the table');
//}
// apply preInsert features
$this->featureSet->apply('preInsert', array($insert));
$statement = $this->sql->prepareStatementForSqlObject($insert);
$result = $statement->execute();
$this->lastInsertValue = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue();
// apply postInsert features
$this->featureSet->apply('postInsert', array($statement, $result));
return $result->getAffectedRows();
}
protected function executeUpdate(Update $update)
{
$updateState = $update->getRawState();
//if ($updateState['table'] != $this->table) {
// throw new Exception\RuntimeException('The table name of the provided Update object must match that of the table');
//}
// apply preUpdate features
$this->featureSet->apply('preUpdate', array($update));
$statement = $this->sql->prepareStatementForSqlObject($update);
$result = $statement->execute();
// apply postUpdate features
$this->featureSet->apply('postUpdate', array($statement, $result));
return $result->getAffectedRows();
}
protected function executeDelete(Delete $delete)
{
$deleteState = $delete->getRawState();
//if ($deleteState['table'] != $this->table) {
// throw new Exception\RuntimeException('The table name of the provided Update object must match that of the table');
//}
// pre delete update
$this->featureSet->apply('preDelete', array($delete));
$statement = $this->sql->prepareStatementForSqlObject($delete);
$result = $statement->execute();
// apply postDelete features
$this->featureSet->apply('postDelete', array($statement, $result));
return $result->getAffectedRows();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment