Skip to content
This repository has been archived by the owner on Oct 26, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Update the Table Gateway Interface
  • Loading branch information
odan committed Oct 15, 2017
1 parent a4cee0e commit ee66fa1
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 45 deletions.
11 changes: 0 additions & 11 deletions .codeclimate.yml

This file was deleted.

1 change: 0 additions & 1 deletion .coveralls.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .editorconfig
Expand Up @@ -17,3 +17,6 @@ end_of_line = crlf
[*.yml]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
20 changes: 11 additions & 9 deletions .gitattributes
@@ -1,3 +1,6 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

public/* linguist-vendored
docs/* linguist-documentation

Expand All @@ -23,12 +26,11 @@ docs/* linguist-documentation
*.woff binary
*.woff2 binary

# ------------------------------------------------------------------------------
# All the files and directories that can be excluded from dist,
# we could have a more clean vendor/
#
# So when someone will install that package through with --prefer-dist option,
# all the files and directories listed in .gitattributes file will be excluded.
# This could have a big impact on big deployments and/or testing.
# ------------------------------------------------------------------------------

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/tests export-ignore
/docs export-ignore
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -2,7 +2,6 @@ language: php

php:
- 7.1
- nightly

# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
Expand Down
19 changes: 17 additions & 2 deletions src/Repository/AbstractRepository.php
Expand Up @@ -2,13 +2,28 @@

namespace App\Repository;

use App\Table\TableInterface;

/**
* Repository
* Repositories The Right Way
*
* Implement separate database logic functions for all your needs inside
* the specific repositories, so your service classes/controllers end up looking like this:
*
* $user = $userRepository->findByUsername('admin');
* $users = $userRepository->findAdminIdsCreatedBeforeDate('2016-01-18 19:21:20');
* $posts = $postRepository->chunkFilledPostsBeforeDate('2016-01-18 19:21:20');
*
* This way all the database logic is moved to the specific repository and I can type hint
* it's returned models. This methodology also results in cleaner easier to read
* code and further separates your core logic from the ORM / query builder.
*/
abstract class AbstractRepository implements RepositoryInterface
{
/**
* @var mixed
* The Table Gateway object
*
* @var TableInterface
*/
protected $table;
}
2 changes: 1 addition & 1 deletion src/Service/User/UserRepository.php
Expand Up @@ -78,7 +78,7 @@ public function getById($id)
*/
public function findByUsername($username)
{
$row = $this->table->newQuery()->columns('*')
$row = $this->table->select()->columns('*')
->where('username', '=', $username)
->where('disabled', '=', 0)
->execute()
Expand Down
44 changes: 25 additions & 19 deletions src/Table/AbstractTable.php
Expand Up @@ -9,18 +9,13 @@
use Odan\Database\UpdateQuery;

/**
* Repositories The Right Way
* Table Gateways
*
* Implement separate database logic functions for all your needs inside
* the specific repositories, so your service classes/controllers end up looking like this:
* The Table Gateway subcomponent provides an object-oriented representation of a database table;
* its methods mirror the most common table operations. In code, the interface resembles.
*
* $user = $userRepository->findByUsername('admin');
* $users = $userRepository->findAdminIdsCreatedBeforeDate('2016-01-18 19:21:20');
* $posts = $postRepository->chunkFilledPostsBeforeDate('2016-01-18 19:21:20');
*
* This way all the database logic is moved to the specific repository and I can type hint
* it's returned models. This methodology also results in cleaner easier to read
* code and further separates your core logic from the ORM / query builder.
* Out of the box, this implementation makes no assumptions about table structure or metadata,
* and when select() is executed, a simple SelectQuery object will be returned.
*/
abstract class AbstractTable implements TableInterface
{
Expand Down Expand Up @@ -48,12 +43,23 @@ public function __construct(Connection $db)
$this->db = $db;
}

/**
* Return the table name.
*
* @return string The table name
*/
public function getTable(): string
{
return $this->tableName;
}


/**
* Create a new select query instance for this table.
*
* @return SelectQuery The query instance
*/
public function newQuery()
public function select(): SelectQuery
{
return $this->db->select()->from($this->tableName);
}
Expand All @@ -66,7 +72,7 @@ public function newQuery()
*/
public function fetchById($id)
{
return $this->newQuery()->columns('*')->where('id', '=', $id)->execute()->fetch();
return $this->select()->columns('*')->where('id', '=', $id)->execute()->fetch();
}

/**
Expand All @@ -76,20 +82,20 @@ public function fetchById($id)
*/
public function fetchAll()
{
return $this->newQuery()->columns('*')->execute()->fetchAll();
return $this->select()->columns('*')->execute()->fetchAll();
}

/**
* Insert a row into the given table name using the key value pairs of data.
*
* @param array|null $data The row data
* @param array|null $row The row data
* @return InsertQuery Statement
*/
public function insert(array $data = null): InsertQuery
public function insert(array $row = null): InsertQuery
{
$insert = $this->db->insert()->into($this->tableName);
if ($data) {
$insert->set($data);
if ($row) {
$insert->set($row);
}
return $insert;
}
Expand All @@ -98,7 +104,7 @@ public function insert(array $data = null): InsertQuery
* Update of a single row in the table.
*
* @param array $row The actual row data that needs to be saved
* @param int|string|array $conditions Id or conditions
* @param int|string|array $conditions The ID or the WHERE conditions
* @return UpdateQuery Statement
*/
public function update(array $row, $conditions): UpdateQuery
Expand All @@ -119,7 +125,7 @@ public function update(array $row, $conditions): UpdateQuery
/**
* Delete a single entity.
*
* @param int|array $conditions ID or conditions
* @param int|array $conditions The ID or the WHERE conditions
* @return DeleteQuery Statement
*/
public function delete($conditions): DeleteQuery
Expand Down
16 changes: 15 additions & 1 deletion src/Table/TableInterface.php
Expand Up @@ -2,9 +2,23 @@

namespace App\Table;

use Odan\Database\DeleteQuery;
use Odan\Database\InsertQuery;
use Odan\Database\SelectQuery;
use Odan\Database\UpdateQuery;

/**
* Table interface
* The Table Gateway Interface
*/
interface TableInterface
{
public function getTable(): string;

public function select(): SelectQuery;

public function insert(array $row = null): InsertQuery;

public function update(array $row, $conditions): UpdateQuery;

public function delete($conditions): DeleteQuery;
}

0 comments on commit ee66fa1

Please sign in to comment.