From f7d2431160dc8144f76107b837f40ce532ae4c92 Mon Sep 17 00:00:00 2001 From: Matthew Baggett Date: Sun, 22 Jan 2023 20:34:23 +0100 Subject: [PATCH] Big changes regarding database access. --- src/Abstracts/AbstractModel.php | 6 ++ src/Abstracts/AbstractTableGateway.php | 92 ++++------------- src/Finder.php | 82 +++++++++++++++ .../Templates/Models/basemodel.php.twig | 16 +++ .../Templates/Models/basetable.php.twig | 8 ++ .../Templates/Services/baseservice.php.twig | 99 +++++++------------ 6 files changed, 166 insertions(+), 137 deletions(-) create mode 100644 src/Finder.php diff --git a/src/Abstracts/AbstractModel.php b/src/Abstracts/AbstractModel.php index 289e0ec..4e791df 100644 --- a/src/Abstracts/AbstractModel.php +++ b/src/Abstracts/AbstractModel.php @@ -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 @@ protected function getProtectedMethods(): array { return ['getPrimaryKeys', 'getProtectedMethods', 'getDIContainer']; } + + abstract public static function find(Finder $finder); + + abstract public static function findOne(Finder $finder); + } diff --git a/src/Abstracts/AbstractTableGateway.php b/src/Abstracts/AbstractTableGateway.php index 8a06574..bade411 100644 --- a/src/Abstracts/AbstractTableGateway.php +++ b/src/Abstracts/AbstractTableGateway.php @@ -5,6 +5,7 @@ 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 @@ public function getByField($field, $value, $orderBy = null, $orderDirection = Se 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 @@ public function getByPrimaryKey(array $primaryKeys) 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; diff --git a/src/Finder.php b/src/Finder.php new file mode 100644 index 0000000..f2e3776 --- /dev/null +++ b/src/Finder.php @@ -0,0 +1,82 @@ +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; + } + + + +} \ No newline at end of file diff --git a/src/Generator/Templates/Models/basemodel.php.twig b/src/Generator/Templates/Models/basemodel.php.twig index 699a344..cb3b205 100644 --- a/src/Generator/Templates/Models/basemodel.php.twig +++ b/src/Generator/Templates/Models/basemodel.php.twig @@ -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; + } } diff --git a/src/Generator/Templates/Models/basetable.php.twig b/src/Generator/Templates/Models/basetable.php.twig index 8a9809e..63df9e5 100644 --- a/src/Generator/Templates/Models/basetable.php.twig +++ b/src/Generator/Templates/Models/basetable.php.twig @@ -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)); + } } diff --git a/src/Generator/Templates/Services/baseservice.php.twig b/src/Generator/Templates/Services/baseservice.php.twig index 8227844..93c7f37 100644 --- a/src/Generator/Templates/Services/baseservice.php.twig +++ b/src/Generator/Templates/Services/baseservice.php.twig @@ -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,42 +228,13 @@ abstract class AbstractBase{{ class_name }}Service extends AbstractService imple */ public function getRandom(): ?Models\{{ class_name }}Model { - ${{ variable_name }}Table = $this->getNewTableGatewayInstance(); + trigger_error("Deprecated function getRandom called. please replace with getByFinder.", E_USER_DEPRECATED); - return ${{ variable_name }}Table->fetchRandom(); - } + //@todo refactor this to internally use getByFinder - /** - * @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); + return ${{ variable_name }}Table->fetchRandom(); } /** @@ -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 }}';