Skip to content

Commit

Permalink
Moved TableGateway class into Application namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
inghamn committed Feb 19, 2019
1 parent db2c981 commit 6ef0f84
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 20 deletions.
2 changes: 1 addition & 1 deletion crm/scripts/solr/generateTicketClusters.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
use Application\Models\Ticket;
use Application\Models\GeoCluster;
use Blossom\Classes\Database;
use Application\Database;

include '../../bootstrap.inc';

Expand Down
2 changes: 1 addition & 1 deletion crm/scripts/solr/indexSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
use Application\Models\Search;
use Application\Models\Ticket;
use Blossom\Classes\Database;
use Application\Database;

include '../../bootstrap.inc';
$search = new Search();
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/ActionTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class ActionTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/AddressTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class AddressTable extends TableGateway
Expand Down
4 changes: 3 additions & 1 deletion crm/src/Application/Models/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public function __construct($id=null)
else {
// This is where the code goes to generate a new, empty instance.
// Set any default values for properties that need it here
$this->setPerson_id($_SESSION['USER']->getId());
if (isset($_SESSION['USER'])) {
$this->setPerson_id($_SESSION['USER']->getId());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/BookmarkTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class BookmarkTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/CategoryGroupTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class CategoryGroupTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/CategoryTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class CategoryTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/ClientTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class ClientTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/ContactMethodTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class ContactMethodTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/DepartmentTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class DepartmentTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/EmailTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class EmailTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/IssueTypeTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class IssueTypeTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/MediaTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class MediaTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/PersonTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/PhoneTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class PhoneTable extends TableGateway
Expand Down
4 changes: 2 additions & 2 deletions crm/src/Application/Models/ResponseTemplateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class ResponseTemplateTable extends TableGateway
{
public function __construct() { parent::__construct('category_action_responses', __namespace__.'\ResponseTemplate'); }
}
}
2 changes: 1 addition & 1 deletion crm/src/Application/Models/SubstatusTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Application\Models;

use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class SubstatusTable extends TableGateway
Expand Down
2 changes: 1 addition & 1 deletion crm/src/Application/Models/TicketTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Application\Models;

use Blossom\Classes\ActiveRecord;
use Blossom\Classes\TableGateway;
use Application\TableGateway;
use Zend\Db\Sql\Select;

class TicketTable extends TableGateway
Expand Down
123 changes: 123 additions & 0 deletions crm/src/Application/TableGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* A base class that streamlines creation of ZF2 TableGateway
*
* @copyright 2014-2019 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE
*/
namespace Application;

use Zend\Db\TableGateway\TableGateway as ZendTableGateway;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

abstract class TableGateway
{
protected $resultSetPrototype;
protected $tableGateway;

/**
* @param string $table The name of the database table
* @param string $class The class model to use as a resultSetPrototype
*
* You must pass in the fully namespaced classname. We do not assume
* any particular namespace for the models.
*/
public function __construct($table, $class)
{
$this->resultSetPrototype = new ResultSet();
$this->resultSetPrototype->setArrayObjectPrototype(new $class());
$this->tableGateway = new ZendTableGateway(
$table,
Database::getConnection(),
null,
$this->resultSetPrototype
);
}

/**
* Simple, default implementation for find
*
* This will allow you to do queries for rows in the table,
* where you provide field=>values for the where clause.
* Only fields actually in the table can be included this way.
*
* You generally want to override this implementation with your own
* However, this basic implementation will allow you to get up and
* running quicker.
*
* @param array $fields Key value pairs to select on
* @param string $order The default ordering to use for select
* @param boolean $paginated If set to true, will return a paginator
* @param int $limit
*/
public function find($fields=null, $order=null, $paginated=false, $limit=null)
{
$select = new Select($this->tableGateway->getTable());
if ($fields) {
foreach ($fields as $key=>$value) {
if (isset($this->columns)) {
if (in_array($key, $this->columns)) {
$select->where([$key=>$value]);
}
}
else {
$select->where([$key=>$value]);
}
}
}
return $this->performSelect($select, $order, $paginated, $limit);
}

/**
* @param Zend\Db\Sql\Select $select
* @return Zend\Db\ResultSet
*/
public function performSelect(Select $select, $order, $paginated=false, $limit=null)
{
if ($order) { $select->order($order); }
if ($limit) { $select->limit($limit); }

if ($paginated) {
$adapter = new DbSelect($select, $this->tableGateway->getAdapter(), $this->resultSetPrototype);
$paginator = new Paginator($adapter);
return $paginator;
}
else {
return $this->tableGateway->selectWith($select);
}
}

/**
* @param Zend\Db\ResultSet
* @return array
*/
public static function hydrateResults(ResultSet $results)
{
$output = [];
foreach ($results as $object) {
$output[] = $object;
}
return $output;
}

/**
* Returns the generated sql
*
* @param Zend\Db\Sql\Select
*/
public function getSqlForSelect(Select $select)
{
return $select->getSqlString($this->tableGateway->getAdapter()->getPlatform());
}

/**
* @param Zend\Db\ResultSet
*/
public static function getSqlForResult(ResultSet $result)
{
return $result->getDataSource()->getResource()->queryString;
}
}
32 changes: 32 additions & 0 deletions crm/src/Test/Integration/ModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* @copyright 2019 City of Bloomington, Indiana
* @license https://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE
*/
declare (strict_types=1);
namespace Test\Integration;

use PHPUnit\Framework\TestCase;

class ModelTest extends TestCase
{
public function classProvider()
{
$classes = [];
foreach (glob(APPLICATION_HOME.'/src/Application/Models/*Table.php') as $f) {
preg_match('/(^.*)\.([^\.]+)$/', $f, $matches);
$file = basename($matches[1]);
$classes[] = ["Application\Models\\".basename($matches[1])];
}
return $classes;
}

/**
* @dataProvider classProvider
*/
public function testTableClasses(string $class)
{
$o = new $class();
$this->assertEquals($class, get_class($o));
}
}

0 comments on commit 6ef0f84

Please sign in to comment.