Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add class to easily build search queries for business models #4

Open
lastzero opened this issue Jan 3, 2019 · 0 comments
Open

Add class to easily build search queries for business models #4

lastzero opened this issue Jan 3, 2019 · 0 comments
Labels
help wanted Good for new / external contributors todo Ready for implementation

Comments

@lastzero
Copy link
Member

lastzero commented Jan 3, 2019

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 <michael@liquidbytes.net>
 * @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 lastzero added todo Ready for implementation help wanted Good for new / external contributors labels Jan 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Good for new / external contributors todo Ready for implementation
Development

No branches or pull requests

1 participant