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

[RFC] new interface for searching #16

Closed
jippi opened this issue May 19, 2015 · 8 comments
Closed

[RFC] new interface for searching #16

jippi opened this issue May 19, 2015 · 8 comments

Comments

@jippi
Copy link
Member

jippi commented May 19, 2015

(Originally posted by @burzum on the wiki)

Instead of relying on the presence of searchConfiguration() in the table class I think a trait that adds a filters() method that returns a manager instance would be better. The interface could be similar to validation:

# Inside the Table instance

// Add filters to the "index" collection
$this->filters('index')
    ->add('field_name', 'like', $options)
    ->add('other_field', 'value', $options)

// Add filters to "otherName"
$this->filters('otherName')
    ->add('field_name', 'compare', $options);

// Returns all filters for the "index" collection
$this->filters('index')->get();

# Inside the Controller instance

// The find call could look like this then for example
 $query = $this->Country
    ->find('search', [$this->request->query, 'index'])
    ->where(['name !=' => null])
    ->order(['Country.id' => 'asc'])
    ->contain([
        'Cities'
    ]);

// Or maybe this?
 $query = $this->Country
    ->applyFilter('index', $this->request->query)
    ->where(['name !=' => null])
    ->order(['Country.id' => 'asc'])
    ->contain([
        'Cities'
    ]);
@burzum
Copy link
Contributor

burzum commented May 22, 2015

@jippi since there is such an overwhelming interest from other people here is my implementation.

Please let me know if this will work for you and I'll add it to my branch.

/**
 * Gets all filters in a given collection.
 *
 * @param string $collection Name of the filter collection.
 * @return array Array of filter instances.
 */
    public function getFilters($collection = 'default')
    {
        return $this->_filters[$collection];
    }

/**
 * Sets or gets the filter collection name.
 *
 * @param string $name Name of the active filter collection to set.
 * @return mixed Returns $this or the name of the active collection if no $name was provided.
 */
    public function collection($name = null) {
        if ($name === null) {
            return $this->_collection;
        }
        if (!isset($this->_filters[$name])) {
            $this->_filters[$name] = [];
        }
        $this->_collection = $name;
        return $this;
    }

/**
 * Adds a new filter to the active collection.
 *
 * @param string $name
 * @param string $filter
 * @param array $options
 * @return $this
 */
    public function add($name, $filter, array $options = [])
    {
        $this->_filters[$this->_collection][$name] = $this->_loadFilter($filter, $options);
        return $this;
    }

/**
 * Loads a search filter instance.
 *
 * @param string $name Name of the filter class to load.
 * @param array $options Filter options.
 * @return \Search\Search\Type\Base
 * @throws \InvalidArgumentException When no filter was found.
 */
    public function _loadFilter($name, array $options = [])
    {
        list($plugin, $name) = pluginSplit($name);
        if (!empty($plugin)) {
            $className = '\\' . $plugin . '\Search\Type\\' . $name;
            if (class_exists($className)) {
                return new $className($name, $options, $this);
            }
        }
        if (isset($config['typeClasses'][$name])) {
            return new $config['typeClasses'][$name]($name, $options, $this);
        }
        if (class_exists('\FOC\Search\Search\Type\\' . $name)) {
            $className = '\FOC\Search\Search\Type\\' . $name;
            return new $className($name, $options, $this);
        }
        if (class_exists('\App\Search\Type\\' . $name)) {
            $className = '\App\Search\Type\\' . $name;
            return new $className($name, $options, $this);
        }
        throw new \InvalidArgumentException(sprintf('Can\'t find filter class %s!', $name));
    }

@burzum
Copy link
Contributor

burzum commented Jul 1, 2015

@jippi I've implented this proposal in my fork of the plugin, feel free to take it from there or just close this ticket.

@davidyell
Copy link
Member

Why a trait and not a behaviour? It makes more sense to be that if it's dealing with the table classes it should be a behaviour. Using a trait feels like a hack to me, like it's sidestepping the framework.

@burzum
Copy link
Contributor

burzum commented Jul 9, 2015

@davidyell where do you see a trait?

@davidyell
Copy link
Member

Sorry, I should have tagged Jippi,
@jippi said

in the table class I think a trait that

Oh, but it looks like it's quoting you. So my question was why use a trait to return the manager rather than a behaviour.

@burzum
Copy link
Contributor

burzum commented Oct 25, 2015

@jippi you can close this.

@burzum
Copy link
Contributor

burzum commented Nov 4, 2015

See #46

@ADmad
Copy link
Member

ADmad commented Nov 4, 2015

Closing as PR is open.

@ADmad ADmad closed this as completed Nov 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants