Big changes regarding database access.
This commit is contained in:
parent
7c431762f3
commit
f7d2431160
6 changed files with 167 additions and 138 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Benzine\ORM\Abstracts;
|
||||
|
||||
use Benzine\ORM\Finder;
|
||||
use Benzine\ORM\Interfaces\ModelInterface;
|
||||
use Camel\CaseTransformer;
|
||||
use Camel\Format;
|
||||
|
|
@ -285,4 +286,9 @@ abstract class AbstractModel implements ModelInterface, \Serializable
|
|||
{
|
||||
return ['getPrimaryKeys', 'getProtectedMethods', 'getDIContainer'];
|
||||
}
|
||||
|
||||
abstract public static function find(Finder $finder);
|
||||
|
||||
abstract public static function findOne(Finder $finder);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ namespace Benzine\ORM\Abstracts;
|
|||
use Benzine\Controllers\Filters\FilterCondition;
|
||||
use Benzine\Exceptions\BenzineException;
|
||||
use Benzine\Exceptions\DbRuntimeException;
|
||||
use Benzine\ORM\Finder;
|
||||
use Benzine\ORM\Interfaces\ModelInterface;
|
||||
use Benzine\ORM\LaminatorSql;
|
||||
use Laminas\Db\Adapter\AdapterInterface;
|
||||
|
|
@ -610,6 +611,22 @@ abstract class AbstractTableGateway extends TableGateway
|
|||
return $row;
|
||||
}
|
||||
|
||||
public function getByFinder(Finder $finder){
|
||||
$select = $this->sql->select();
|
||||
|
||||
$select->where($finder);
|
||||
|
||||
if($finder->getOrder())
|
||||
$select->order($finder->getOrder());
|
||||
|
||||
if($finder->getLimit())
|
||||
$select->limit($finder->getLimit());
|
||||
|
||||
if($finder->getOffset())
|
||||
$select->offset($finder->getOffset());
|
||||
|
||||
return $this->selectWith($select);
|
||||
}
|
||||
/**
|
||||
* @param Where $where
|
||||
* @param null|int $limit
|
||||
|
|
@ -709,81 +726,6 @@ abstract class AbstractTableGateway extends TableGateway
|
|||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get single matching object.
|
||||
*
|
||||
* @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue
|
||||
* @param null $orderBy
|
||||
* @param string $orderDirection
|
||||
*/
|
||||
public function getMatching($keyValue = [], $orderBy = null, $orderDirection = Select::ORDER_ASCENDING)
|
||||
{
|
||||
$select = $this->sql->select();
|
||||
$select->where($keyValue);
|
||||
if ($orderBy) {
|
||||
if ($orderBy instanceof Expression) {
|
||||
$select->order($orderBy);
|
||||
} else {
|
||||
$select->order("{$orderBy} {$orderDirection}");
|
||||
}
|
||||
}
|
||||
$select->limit(1);
|
||||
|
||||
$resultSet = $this->selectWith($select);
|
||||
|
||||
$row = $resultSet->current();
|
||||
if (!$row) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get many matching objects.
|
||||
*
|
||||
* @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue
|
||||
* @param null $orderBy
|
||||
* @param string $orderDirection
|
||||
* @param int $limit
|
||||
*
|
||||
* @return null|array|\ArrayObject
|
||||
*/
|
||||
public function getManyMatching($keyValue = [], $orderBy = null, $orderDirection = Select::ORDER_ASCENDING, int $limit = null): ?array
|
||||
{
|
||||
$select = $this->sql->select();
|
||||
$select->where($keyValue);
|
||||
if ($orderBy) {
|
||||
if ($orderBy instanceof Expression) {
|
||||
$select->order($orderBy);
|
||||
} else {
|
||||
$select->order("{$orderBy} {$orderDirection}");
|
||||
}
|
||||
}
|
||||
if ($limit) {
|
||||
$select->limit($limit);
|
||||
}
|
||||
$resultSet = $this->selectWith($select);
|
||||
|
||||
$results = [];
|
||||
if (0 == $resultSet->count()) {
|
||||
return null;
|
||||
}
|
||||
for ($i = 0; $i < $resultSet->count(); ++$i) {
|
||||
/** @var AbstractModel $row */
|
||||
$row = $resultSet->current();
|
||||
if ($row->hasPrimaryKey()) {
|
||||
$id = implode('-', $row->getPrimaryKeys());
|
||||
$results[$id] = $row;
|
||||
} else {
|
||||
$results[] = $row;
|
||||
}
|
||||
$resultSet->next();
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function getNewModelInstance(array $data = []): AbstractModel
|
||||
{
|
||||
$model = $this->model;
|
||||
|
|
|
|||
82
src/Finder.php
Normal file
82
src/Finder.php
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
namespace Benzine\ORM;
|
||||
|
||||
use Benzine\ORM\Exception\BenzineOrmException;
|
||||
use Laminas\Db\Sql\Predicate\PredicateSet;
|
||||
|
||||
class Finder extends \Laminas\Db\Sql\Where
|
||||
{
|
||||
public function __construct(
|
||||
?array $predicates = null,
|
||||
$defaultCombination = self::COMBINED_BY_AND,
|
||||
private ?int $offset = null,
|
||||
private ?int $limit = null,
|
||||
private ?string $order = null,
|
||||
private string $orderDirection = 'ASC',
|
||||
){
|
||||
parent::__construct($predicates, $defaultCombination);
|
||||
}
|
||||
|
||||
public static function Where(){
|
||||
return new self();
|
||||
}
|
||||
|
||||
|
||||
public function getOffset()
|
||||
{
|
||||
return $this->offset;
|
||||
}
|
||||
|
||||
|
||||
public function offset($offset)
|
||||
{
|
||||
$this->offset = $offset;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLimit()
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
|
||||
public function limit($limit)
|
||||
{
|
||||
$this->limit = $limit;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getOrder() : ?string
|
||||
{
|
||||
if($this->order)
|
||||
return "{$this->order} {$this->orderDirection}";
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public function orderBy($order, $direction =null)
|
||||
{
|
||||
$this->order = $order;
|
||||
if($direction)
|
||||
$this->orderDirection($direction);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $orderDirection
|
||||
* @return Finder
|
||||
*/
|
||||
public function orderDirection(string $orderDirection): Finder
|
||||
{
|
||||
if(!in_array($orderDirection, ['desc','asc'])){
|
||||
throw new BenzineOrmException(sprintf("Order direction must be one of 'desc' or 'asc', given '%s'", $orderDirection));
|
||||
}
|
||||
$this->orderDirection = $orderDirection;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ use {{ namespace }}\Models;
|
|||
use {{ namespace }}\TableGateways;
|
||||
use {{ namespace }}\Services;
|
||||
use DI\NotFoundException;
|
||||
use Benzine\ORM\Finder;
|
||||
use Benzine\ORM\Abstracts\AbstractModel;
|
||||
use Benzine\ORM\Interfaces\ModelInterface as ModelInterface;
|
||||
use Benzine\App as App;
|
||||
|
|
@ -467,4 +468,19 @@ abstract class AbstractBase{{ class_name }}Model extends AbstractModel implement
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns Models\{{ class_name }}Model[]
|
||||
*/
|
||||
public static function find(Finder $finder)
|
||||
{
|
||||
${{ variable_name }}Service = App::DI(Services\{{ class_name }}Service::class);
|
||||
|
||||
return ${{ variable_name }}Service->getByFinder($finder);
|
||||
}
|
||||
|
||||
public static function findOne(Finder $finder) : ?Models\{{ class_name }}Model
|
||||
{
|
||||
$result = self::find($finder->limit(1));
|
||||
return $result[0] ?? null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ use Laminas\Db\Sql\Where;
|
|||
use Benzine\ORM\Abstracts\AbstractModel;
|
||||
use Benzine\ORM\Abstracts\AbstractTableGateway;
|
||||
use Benzine\ORM\Connection;
|
||||
use Benzine\ORM\Finder;
|
||||
use Benzine\ORM\Interfaces\TableGatewayInterface as TableGatewayInterface;
|
||||
use Benzine\Exceptions\BenzineException;
|
||||
use Carbon\Carbon;
|
||||
|
|
@ -189,4 +190,11 @@ abstract class AbstractBase{{ class_name }}TableGateway extends AbstractTableGat
|
|||
$orderDirection
|
||||
);
|
||||
}
|
||||
|
||||
public function getByFinder(Finder $finder)
|
||||
: Collections\{{ class_name }}Collection
|
||||
{
|
||||
return (new Collections\{{ class_name }}Collection())
|
||||
->fromResultSet(parent::getByFinder($finder));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use Laminas\Db\Sql\Expression;
|
|||
use Laminas\Db\Sql\Select;
|
||||
use Laminas\Db\Sql\Predicate;
|
||||
use Laminas\Db\Sql\Where;
|
||||
use Benzine\ORM\Finder;
|
||||
use Benzine\ORM\Abstracts\AbstractService;
|
||||
use Benzine\ORM\Interfaces\ServiceInterface as ServiceInterface;
|
||||
|
||||
|
|
@ -47,6 +48,7 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
}
|
||||
|
||||
/**
|
||||
* @deprecated Please replace with getByFinder
|
||||
* @param null|int $limit
|
||||
* @param null|int $offset
|
||||
* @param null|array|\Closure[] $wheres
|
||||
|
|
@ -62,6 +64,9 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
$order = null,
|
||||
string $orderDirection = null
|
||||
) : Collections\{{ class_name }}Collection {
|
||||
|
||||
trigger_error("Deprecated function getAll called. please replace with getByFinder.", E_USER_DEPRECATED);
|
||||
|
||||
return parent::getAll(
|
||||
$limit,
|
||||
$offset,
|
||||
|
|
@ -90,6 +95,7 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
{% endfor %}
|
||||
|
||||
/**
|
||||
* @deprecated Please use getByFinder instead.
|
||||
* @param string $field Table column to filter by
|
||||
* @param mixed $value Data to filter by
|
||||
* @param null|string $orderBy Field to sort by
|
||||
|
|
@ -99,12 +105,15 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
*/
|
||||
public function getByField(string $field, $value, $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?Models\{{ class_name }}Model
|
||||
{
|
||||
trigger_error("Deprecated function getByField called. please replace with getByFinder.", E_USER_DEPRECATED);
|
||||
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name }}Table->getByField($field, $value, $orderBy, $orderDirection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Please use getByFinder instead.
|
||||
* @param Where $where Laminas Where statement to use
|
||||
* @param null|int $limit
|
||||
* @param null|int $offset
|
||||
|
|
@ -120,12 +129,15 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
$orderBy = null,
|
||||
$orderDirection = Select::ORDER_ASCENDING
|
||||
): Collections\{{ class_name }}Collection {
|
||||
trigger_error("Deprecated function getManyByWhere called. please replace with getByFinder.", E_USER_DEPRECATED);
|
||||
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name }}Table->getManyByWhere($where, $limit, $offset, $orderBy, $orderDirection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Please use getByFinder instead, which is drop-in compatable.
|
||||
* @param Where $where Laminas Where statement to use
|
||||
*
|
||||
* @return null|Models\{{ class_name }}Model
|
||||
|
|
@ -134,6 +146,7 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
Where $where
|
||||
): ?Models\{{ class_name }}Model
|
||||
{
|
||||
trigger_error("Deprecated function getByWhere called. please replace with getByFinder.", E_USER_DEPRECATED);
|
||||
${{ variable_name }} = $this->getManyByWhere($where, 1);
|
||||
if (${{ variable_name }}->count() == 0) {
|
||||
return null;
|
||||
|
|
@ -142,15 +155,27 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
return ${{ variable_name }}[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collections\{{ class_name }}Collection|Models\{{ class_name }}Model[]
|
||||
*/
|
||||
public function getByFinder(Finder $finder)
|
||||
: Collections\{{ class_name }}Collection
|
||||
{
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name}}Table->getByFinder($finder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field Table column to filter by
|
||||
* @param mixed $value Data to filter by
|
||||
* @deprecated Please use getByFinder instead.
|
||||
* @param null|int $limit
|
||||
* @param null|int $offset
|
||||
* @param null|string $orderBy Field to sort by
|
||||
* @param null|string $orderDirection Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING)
|
||||
*
|
||||
* @return Collections\{{ class_name }}Collection|Models\{{ class_name }}Model[]
|
||||
* @return Collections\{{ class_name }}Collection|Models\{{ class_name }}Model[]
|
||||
*/
|
||||
public function getManyByField(
|
||||
string $field,
|
||||
|
|
@ -160,12 +185,15 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
$orderBy = null,
|
||||
$orderDirection = Select::ORDER_ASCENDING
|
||||
) : Collections\{{ class_name }}Collection {
|
||||
trigger_error("Deprecated function getManyByField called. please replace with getByFinder.", E_USER_DEPRECATED);
|
||||
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name }}Table->getManyByField($field, $value, $limit, $offset, $orderBy, $orderDirection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Please use countByFinder instead.
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
*
|
||||
|
|
@ -173,18 +201,23 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
*/
|
||||
public function countByField(string $field, $value): int
|
||||
{
|
||||
trigger_error("Deprecated function countByField called. please replace with countByFinder.", E_USER_DEPRECATED);
|
||||
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name }}Table->countByField($field, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Please use countByFinder instead (and is drop-in compatable).
|
||||
* @param Where $where
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countByWhere(Where $where): int
|
||||
{
|
||||
trigger_error("Deprecated function countByWhere called. please replace with countByFinder.", E_USER_DEPRECATED);
|
||||
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name }}Table->countByWhere($where);
|
||||
|
|
@ -195,44 +228,15 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
*/
|
||||
public function getRandom(): ?Models\{{ class_name }}Model
|
||||
{
|
||||
trigger_error("Deprecated function getRandom called. please replace with getByFinder.", E_USER_DEPRECATED);
|
||||
|
||||
//@todo refactor this to internally use getByFinder
|
||||
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name }}Table->fetchRandom();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue
|
||||
* @param string $orderBy Field to sort by
|
||||
* @param string $orderDirection Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING)
|
||||
*
|
||||
* @return Models\{{ class_name }}Model
|
||||
*/
|
||||
public function getMatching($keyValue = [], $orderBy = null, $orderDirection = Select::ORDER_ASCENDING): ?Models\{{ class_name }}Model
|
||||
{
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name }}Table->getMatching($keyValue, $orderBy, $orderDirection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|\Closure|Predicate\PredicateInterface|string|Where $keyValue
|
||||
* @param string $orderBy Field to sort by
|
||||
* @param string $orderDirection Direction to sort (Select::ORDER_ASCENDING || Select::ORDER_DESCENDING)
|
||||
* @param int $limit Limit the number of matches returned
|
||||
*
|
||||
* @return Collections\{{ class_name }}Collection|Models\{{ class_name }}Model[]
|
||||
*/
|
||||
public function getManyMatching(
|
||||
$keyValue = [],
|
||||
$orderBy = null,
|
||||
$orderDirection = Select::ORDER_ASCENDING,
|
||||
int $limit = null
|
||||
) : Collections\{{ class_name }}Collection {
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name }}Table->getManyMatching($keyValue, $orderBy, $orderDirection, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $dataExchange
|
||||
*
|
||||
|
|
@ -246,35 +250,6 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple
|
|||
return ${{ variable_name }}Table->save(${{ variable_name }});
|
||||
}
|
||||
|
||||
{% for column in columns %}
|
||||
{% if column.getFieldSanitised == 'id' %}
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @deprecated
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function deleteById($id): int
|
||||
{
|
||||
return $this->deleteByField('id', $id);
|
||||
}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function deleteByField(string $field, $value): int
|
||||
{
|
||||
${{ variable_name }}Table = $this->getNewTableGatewayInstance();
|
||||
|
||||
return ${{ variable_name }}Table->delete([$field => $value]);
|
||||
}
|
||||
|
||||
public function getTermPlural(): string
|
||||
{
|
||||
return '{{ object_name_plural }}';
|
||||
|
|
|
|||
Loading…
Reference in a new issue