The basis for every query object
Returns a key => value array representing a single aliased field that can be passed directly to the select() method. The key will contain the alias and the value the actual field name.
Runs aliasField()
for each field in the provided list and returns the result under a single array.
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:
Returns the first result out of executing this query, if the query has not been executed before, it will set the limit clause to 1 for performance reasons.
Sets the number of records that should be retrieved from database, accepts an integer or an expression object that evaluates to an integer. In some databases, this operation might not be supported or will require the query to be transformed in order to limit the result set size.
Sets the number of records that should be skipped from the original result set This is commonly used for paginating large results. Accepts an integer or an expression object that evaluates to an integer.
Adds a single or multiple fields to be used in the ORDER clause for this query. Fields can be passed as an array of strings, array of expression objects, a single expression or a single string.
Returns the default repository object that will be used by this query, that is, the repository that will appear in the from clause.
Adds a condition or set of conditions to be used in the WHERE clause for this query. Conditions can be expressed as an array of fields as keys with comparison operators in it, the values for the array will be used for comparing the field to such literal. Finally, conditions can be expressed as a single string or an array of strings.
aliasField( string $field , string|null $alias null )
Returns a key => value array representing a single aliased field that can be passed directly to the select() method. The key will contain the alias and the value the actual field name.
If the field is already aliased, then it will not be changed. If no $alias is passed, the default table for this query will be used.
$field
$alias
optional null aliasFields( array $fields , string|null $defaultAlias null )
Runs aliasField()
for each field in the provided list and returns the result under a single array.
$fields
$defaultAlias
optional null all( )
Fetch the results for this query.
Will return either the results set through setResult(), or execute this query and return the ResultSetDecorator object ready for streaming of results.
ResultSetDecorator is a traversable object that implements the methods found on Cake\Collection\Collection.
Cake\Datasource\ResultSetInterface
applyOptions( array $options )
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:
$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)
$options
find( string $finder , array $options [] )
Apply custom finds to against an existing query object.
Allows custom find methods to be combined and applied to each other.
$repository->find('all')->find('recent');
The above is an example of stacking multiple finder methods onto a single query.
$finder
$options
optional [] first( )
Returns the first result out of executing this query, if the query has not been executed before, it will set the limit clause to 1 for performance reasons.
$singleUser = $query->select(['id', 'username'])->first();
limit( integer $num )
Sets the number of records that should be retrieved from database, accepts an integer or an expression object that evaluates to an integer. In some databases, this operation might not be supported or will require the query to be transformed in order to limit the result set size.
$query->limit(10) // generates LIMIT 10 $query->limit($query->newExpr()->add(['1 + 1'])); // LIMIT (1 + 1)
$num
offset( integer $num )
Sets the number of records that should be skipped from the original result set This is commonly used for paginating large results. Accepts an integer or an expression object that evaluates to an integer.
In some databases, this operation might not be supported or will require the query to be transformed in order to limit the result set size.
$query->offset(10) // generates OFFSET 10 $query->offset($query->newExpr()->add(['1 + 1'])); // OFFSET (1 + 1)
$num
order( array|string $fields , boolean $overwrite false )
Adds a single or multiple fields to be used in the ORDER clause for this query. Fields can be passed as an array of strings, array of expression objects, a single expression or a single string.
If an array is passed, keys will be used as the field itself and the value will represent the order in which such field should be ordered. When called multiple times with the same fields as key, the last order definition will prevail over the others.
By default this function will append any passed argument to the list of fields to be selected, unless the second argument is set to true.
$query->order(['title' => 'DESC', 'author_id' => 'ASC']);
Produces:
ORDER BY title DESC, author_id ASC
$query->order(['title' => 'DESC NULLS FIRST'])->order('author_id');
Will generate:
ORDER BY title DESC NULLS FIRST, author_id
$expression = $query->newExpr()->add(['id % 2 = 0']); $query->order($expression)->order(['title' => 'ASC']);
Will become:
ORDER BY (id %2 = 0), title ASC
If you need to set complex expressions as order conditions, you should use orderAsc()
or orderDesc()
.
$fields
$overwrite
optional false page( integer $num , integer|null $limit null )
Set the page of results you want.
This method provides an easier to use interface to set the limit + offset in the record set you want as results. If empty the limit will default to the existing limit clause, and if that too is empty, then 25
will be used.
Pages should start at 1.
$num
$limit
optional null The number of rows you want in the page. If null the current limit clause will be used.
repository( Cake\Datasource\RepositoryInterface $repository null )
Returns the default repository object that will be used by this query, that is, the repository that will appear in the from clause.
Cake\Datasource\RepositoryInterface
$repository
optional null Cake\Datasource\RepositoryInterface
|Cake\Datasource\QueryInterface
toArray( )
Returns an array representation of the results after executing the query.
where( string|array|callable|null $conditions null , array $types [] , boolean $overwrite false )
Adds a condition or set of conditions to be used in the WHERE clause for this query. Conditions can be expressed as an array of fields as keys with comparison operators in it, the values for the array will be used for comparing the field to such literal. Finally, conditions can be expressed as a single string or an array of strings.
When using arrays, each entry will be joined to the rest of the conditions using an AND operator. Consecutive calls to this function will also join the new conditions specified using the AND operator. Additionally, values can be expressed using expression objects which can include other query objects.
Any conditions created with this methods can be used with any SELECT, UPDATE and DELETE type of queries.
$query->where([ 'posted >=' => new DateTime('3 days ago'), 'title LIKE' => 'Hello W%', 'author_id' => 1, ], ['posted' => 'datetime']);
The previous example produces:
WHERE posted >= 2012-01-27 AND title LIKE 'Hello W%' AND author_id = 1
Second parameter is used to specify what type is expected for each passed key. Valid types can be used from the mapped with Database\Type class.
$query->where([ 'author_id !=' => 1, 'OR' => ['published' => true, 'posted <' => new DateTime('now')], 'NOT' => ['title' => 'Hello'] ], ['published' => boolean, 'posted' => 'datetime']
The previous example produces:
WHERE author_id = 1 AND (published = 1 OR posted < '2012-02-01') AND NOT (title = 'Hello')
You can nest conditions using conjunctions as much as you like. Sometimes, you may want to define 2 different options for the same key, in that case, you can wrap each condition inside a new array:
$query->where(['OR' => [['published' => false], ['published' => true]])
Keep in mind that every time you call where() with the third param set to false (default), it will join the passed conditions to the previous stored list using the AND operator. Also, using the same array key twice in consecutive calls to this method will not override the previous value.
$exp = $query->newExpr()->add(['id !=' => 100, 'author_id' != 1])->tieWith('OR'); $query->where(['published' => true], ['published' => 'boolean'])->where($exp);
The previous example produces:
WHERE (id != 100 OR author_id != 1) AND published = 1
Other Query objects that be used as conditions for any field.
You can use callable functions to construct complex expressions, functions receive as first argument a new QueryExpression object and this query instance as second argument. Functions must return an expression object, that will be added the list of conditions for the query using the AND operator.
$query ->where(['title !=' => 'Hello World']) ->where(function ($exp, $query) { $or = $exp->or_(['id' => 1]); $and = $exp->and_(['id >' => 2, 'id <' => 10]); return $or->add($and); });
WHERE title != 'Hello World' AND (id = 1 OR (id > 2 AND id < 10))
$query->where(['articles.author_id = authors.id', 'modified IS NULL']);
The previous example produces:
WHERE articles.author_id = authors.id AND modified IS NULL
Please note that when using the array notation or the expression objects, all values will be correctly quoted and transformed to the correspondent database data type automatically for you, thus securing your application from SQL injections. If you use string conditions make sure that your values are correctly quoted. The safest thing you can do is to never use string conditions.
$conditions
optional null $types
optional [] $overwrite
optional false
© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Datasource.QueryInterface.html