jcshoww/query-collection

查询集合库,用于查询连续构建

1.2.2 2024-01-26 05:48 UTC

This package is auto-updated.

Last update: 2024-09-26 07:09:41 UTC


README

这是一个查询集合库。查询集合可以用作不同查询对象的存储集合,可以用于数据库/API等查询的动态构建和操作。

安装

您可以通过composer安装此包

composer require jcshoww/query-collection

使用方法

创建自己的查询集合,并从基础QueryCollection类扩展它。在自定义集合的__construct方法中添加自定义基本集合生成。

下面是示例

...
use Jcshoww\QueryCollection\QueryCollection;

class TestQueryCollection extends QueryCollection
{
    /**
     * {@inheritDoc}
     */
    public function __construct(array $fields = [])
    {
        $this->push(new CustomFilter('status', $fields['status']));
    }
}

创建自己的自定义查询对象,并从Query类扩展它们。下面是示例

...
use Jcshoww\QueryCollection\Builder\Builder;
use Jcshoww\QueryCollection\Query\Query;

class Join extends Query
{
    /**
     * {@inheritDoc}
     */
    protected $type = 'Join';

    /**
     * table name
     * 
     * @var string
     */
    protected $table;

    /**
     * owner column to join
     * 
     * @var string
     */
    protected $ownerColumn;
    
    /**
     * foreign column to join
     * 
     * @var string
     */
    protected $foreignColumn;

    /**
     * @param string $table
     * @param string $ownerColumn
     * @param string $foreignColumn
     */
    public function __construct(string $table, string $ownerColumn, string $foreignColumn)
    {
        $this->table = $table;
        $this->ownerColumn = $ownerColumn;
        $this->foreignColumn = $foreignColumn;
    }

    /**
     * @param LaravelBuilder $builder
     * 
     * @return Query
     */
    public function apply(Builder $builder): Query
    {
        $builder->join($this->table, $this->ownerColumn, Where::EQUAL, $this->foreignColumn);
        return $builder;
    }
}

创建自己的自定义构建对象,并从Builder类扩展以自定义查询和系统操作之间的完成操作。