applyOptions() public method

Populates or adds parts to current query clauses using an array. 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 - limit: Maps to the limit method - order: Maps to the order method - offset: Maps to the offset method - group: Maps to the group method - having: Maps to the having method - contain: Maps to the contain options for eager loading - join: Maps to the join 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
Example #1
0
 /**
  * Tests getOptions() method
  *
  * @return void
  */
 public function testGetOptions()
 {
     $options = ['doABarrelRoll' => true, 'fields' => ['id', 'name']];
     $query = new Query($this->connection, $this->table);
     $query->applyOptions($options);
     $expected = ['doABarrelRoll' => true];
     $this->assertEquals($expected, $query->getOptions());
     $expected = ['doABarrelRoll' => false, 'doAwesome' => true];
     $query->applyOptions($expected);
     $this->assertEquals($expected, $query->getOptions());
 }
 /**
  * Custom finder that obfuscates primary keys in returned result set.
  *
  * @param \Cake\ORM\Query $query Query.
  * @param array $options Options.
  * @return \Cake\ORM\Query
  */
 public function findObfuscate(Query $query, array $options)
 {
     $query->applyOptions(['obfuscate' => true]);
     $query->formatResults(function ($results) {
         return $results->map(function ($row) {
             $pk = $this->_table->primaryKey();
             $row[$pk] = $this->obfuscate($row[$pk]);
             return $row;
         });
     });
     return $query;
 }
Example #3
0
 /**
  * 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\ORM\Query $query The query object to apply the finder options to
  * @param array $options List of options to pass to the finder
  * @return \Cake\ORM\Query
  * @throws \BadMethodCallException
  */
 public function callFinder($type, Query $query, array $options = [])
 {
     $query->applyOptions($options);
     $options = $query->getOptions();
     $finder = 'find' . $type;
     if (method_exists($this, $finder)) {
         return $this->{$finder}($query, $options);
     }
     if ($this->_behaviors && $this->_behaviors->hasFinder($type)) {
         return $this->_behaviors->callFinder($type, [$query, $options]);
     }
     throw new \BadMethodCallException(sprintf('Unknown finder method "%s"', $type));
 }
 /**
  * Method that filters ORM records by provided conditions.
  *
  * @param  \Cake\ORM\Query   $query Query object
  * @param  \Cake\Event\Event $event The event
  * @return void
  */
 protected function _filterByConditions(Query $query, Event $event)
 {
     if (empty($event->subject()->request->query('conditions'))) {
         return;
     }
     $conditions = [];
     $tableName = $event->subject()->name;
     foreach ($event->subject()->request->query('conditions') as $k => $v) {
         if (false === strpos($k, '.')) {
             $k = $tableName . '.' . $k;
         }
         $conditions[$k] = $v;
     }
     $query->applyOptions(['conditions' => $conditions]);
 }