Cake\ElasticSearch\Query::applyOptions PHP Method

applyOptions() public method

This is handy for passing all query clauses at once. The option array accepts: - fields: Maps to the select method - conditions: Maps to the where method - order: Maps to the order method - limit: Maps to the limit method - offset: Maps to the offset method - page: Maps to the page method ### Example: $query->applyOptions([ 'fields' => ['id', 'name'], 'conditions' => [ 'created >=' => '2013-01-01' ], 'limit' => 10 ]); Is equivalent to: $query ->select(['id', 'name']) ->where(['created >=' => '2013-01-01']) ->limit(10)
public applyOptions ( array $options )
$options array list of query clauses to apply new parts to.
    public function applyOptions(array $options)
    {
        $valid = ['fields' => 'select', 'conditions' => 'where', 'order' => 'order', 'limit' => 'limit', 'offset' => 'offset', 'page' => 'page'];
        ksort($options);
        foreach ($options as $option => $values) {
            if (isset($valid[$option]) && isset($values)) {
                $this->{$valid[$option]}($values);
            } else {
                $this->_options[$option] = $values;
            }
        }
        return $this;
    }

Usage Example

 /**
  * Calls a finder method directly and applies it to the passed query,
  * if no query is passed a new one will be created and returned
  *
  * @param string $type name of the finder to be called
  * @param \Cake\ElasticSearch\Query $query The query object to apply the finder options to
  * @param array $options List of options to pass to the finder
  * @return \Cake\ElasticSearch\Query
  * @throws \BadMethodCallException
  */
 public function callFinder($type, Query $query, $options = [])
 {
     $query->applyOptions($options);
     $options = $query->getOptions();
     $finder = 'find' . ucfirst($type);
     if (method_exists($this, $finder)) {
         return $this->{$finder}($query, $options);
     }
     throw new \BadMethodCallException(sprintf('Unknown finder method "%s"', $type));
 }