doctrine-active-record icon indicating copy to clipboard operation
doctrine-active-record copied to clipboard

Add class to easily build search queries for business models

Open lastzero opened this issue 6 years ago • 0 comments

As a developer, I want an easy-to-use class to create search queries so that I don't need to read a lot of documentation.

Docs: https://docs.symlex.org/en/latest/doctrine-active-record/search/

I've started this to make it easier for developers to create search queries. Right now, you need to manually build an array with all parameters. However, I don't have time to complete it and no real use case to test it. Feel free to pick this up and continue my work! Would be amazing.

<?php

namespace Doctrine\ActiveRecord\Search;

use Doctrine\ActiveRecord\Model\EntityModel;

/**
 * Build search queries for business models
 *
 * @author Michael Mayer <[email protected]>
 * @license MIT
 */
class SearchQuery
{
    protected $model;
    protected $result;
    protected $cond = array();
    protected $tableName = '';
    protected $tableAlias = '';
    protected $count = 20; // Max number of search results
    protected $offset = 0; // Search result offset
    protected $countTotal = true; // Count total number of rows?
    protected $join = false; // Regular joins
    protected $leftJoin = false; // Left joins
    protected $columns = false; // Array of column (false for all)
    protected $order = false;
    protected $group = false;
    protected $wrap = true;
    protected $idsOnly = false;
    protected $sqlFilter = '',
    protected $idFilter = array();

    public function __construct(EntityModel $model)
    {
        $this->model = $model;
        $this->table = $model->getTableName();
    }

    public function addCondition($cond)
    {
        if (is_array($cond)) {
            $this->cond[$key] => $value;
        } else {
            // TODO
        }
    }

    public function setTableName(string $tableName)
    {
        $this->tableName = $tableName;

        return $this;
    }

    public function setTableAlias(string $tableAlias)
    {
        $this->tableAlias = $tableAlias;

        return $this;
    }

    public function setMaxCount(int $count)
    {
        $this->count = $count;

        return $this;
    }

    public function setOffset(int $offset)
    {
        $this->offset = $offset;

        return $this;
    }

    public function setCountTotal(bool $countTotal)
    {
        $this->countTotal = $countTotal;

        return $this;
    }

    public function getOptions()
    {
        $result = array(
            'table' => $this->tableName,
            'table_alias' => $this->tableAlias,
            'count' => $this->count,
            'offset' => $this->offset,
            'count_total' => $this->countTotal,
            'join' => $this->join,
            'left_join' => $this->leftJoin,
            'columns' => $this->columns,
            'order' => $this->order,
            'group' => $this->group,
            'wrap' => $this->wrap,
            'ids_only' => $this->idsOnly,
            'sql_filter' => $this->sqlFilter,
            'id_filter' => $this->idFilter
        );

        return $result;
    }

    public function getCond()
    {
        return $this->cond;
    }

    public function search()
    {
        $this->model->search($this->getCond(), $this->getOptions());

        return $this;
    }

    public function getResult(): SearchResult
    {
        return $this->result;
    }
}

lastzero avatar Jan 03 '19 01:01 lastzero