itseasy/laminas-db

Laminas 数据库包装器

v1.1.0 2022-11-08 09:22 UTC

This package is auto-updated.

Last update: 2024-09-27 09:10:16 UTC


README

Laminas DB 库包装器

RepositoryAbstractServiceFactory

Laminas ServiceManager 的懒加载仓库初始化。任何以 Repository 结尾的服务请求都将自动创建。

要使用它,请将其放入服务配置中的 service_factory

return [
    "service" => [
        "abstract_factories" => [
            Itseasy\Repository\Factory\RepositoryAbstractServiceFactory::class
        ]
    ]
]

GenericRepository

一个通用仓库类,当使用时将由 RepositoryAbstractServiceFactory 调用。

AbstractRepository

抽象仓库类。

任何需要 FilterAware 函数的都必须在构造函数中定义 tablesqlFilter

可用功能

# Get Record by identifer
getRowByIdentifier(
    $value,
    string $identifier = "id",
    $objectPrototype
) : ResultInterface | $objectPrototype;

# Get Multiple Record by array of condition
getRows(
    array $where = [],
	?string $orders = null,
    ?int $offset = null,
    ?int $limit = null,
    $resultSetObjectPrototype = null,
    $objectPrototype = null
) : ResultInterface | $resultSetObjectPrototype | ArrayIterator;

# Get Row Count by array of condition
getRowCount(
    array $where = []
): int;

# Delete Record base on condition
delete(array $where = []): ResultInterface;

# Insert or update existing Record base on identifier
# $model must has a getter and getArrayCopy() function to read properties
public function upsert(
   	object $model,
	string $identifier = "id",
   	array $exclude_attributes = []
) : $model;

# Get Multiple Record base on filter given
# Filter will be converted to sql by predefined filter
getFilterAwareRows(
    string $filters = null,
    ?string $orders = null,
    ?int $offset = null,
    ?int $limit = null,
    $resultSetObjectPrototype = null,
    $objectPrototype = null
) : ResultInterface | $resultSetObjectPrototype | ArrayIterator;

# Get Record Count base on filter given
# Filter will be converted to sql by predefined filter
getFilterAwareRowCount(
    string $filters = null
) : int;

# Delete Record base on filter given
# Filter will be converted to sql by predefined filter
filterAwareDelete(string $filters = null) : ResultInterface;

SqlFilter

一个辅助类,用于根据定义添加基于 SQL 的筛选查询。

RegexSqlFilter

使用基于正则表达式的 SQL 筛选。正则表达式中的圆括号内的值((...))将作为参数传递给回调函数。所有回调都必须返回 Laminas\Db\Sql\Predicate\PredicateInterface

所有筛选器从上到下依次运行

示例

use Itseasy\Repository\AbstractRepository;
use Itseasy\Database\Sql\Filter\RegexSqlFilter;

class Repository extends AbstractRepository
{
    # Can be public / protected function
    protected function defineSqlFilter() : void
    {
        $this->setSqlFilter(new RegexSqlFilter([
            [
                "is:active", function ($status) {
                    $p = new Predicate();
                    return $p->equalTo("active", true);
                }
            ],
            [
                "id:(\d)", function ($id) {
                    $p = new Predicate();
                    return $p->equalTo("id", $id);
                }
            ],
            [
                "tech_creation_date:(\d{4}-\d{2}-\d{2}):(\d{4}-\d{2}-\d{2})", function($start_date, $end_date) {
                    return new Laminas\Db\Sql\Predicate\Between(
                        "tech_creation_date",
                        $start_date,
                        $end_date
                    );
                }
            ],
            [
                "([a-z0-9]+)", function($value) {
                    $value = str_replace(" ", "%", $value);
                	return new Laminas\Db\Sql\PredicateSet([
 	                    new Lamainas|Db\Sql\Predicate\Like("first_name", "%$value%"),
                        new Lamainas|Db\Sql\Predicate\Like("last_name", "%$value%"),
 			            new Lamainas|Db\Sql\Predicate\Like("email", "%$value%"),
                    ], Laminas\Db\Sql\Predicate\PredicateSet::COMBINED_BY_AND );
                }
            ]
        ]));
    }
}

# Usage
$repository = new Repository($db, "mytable");
$repository->getFilterAwareRows("id:12", "id DESC", 0, 10);
$repository->getFilterAwareRows("tech_creation_date:2022-01-01:2022-03-01", null, 0, 10);
$repository->getFilterAwareRows("is:active somebody");