ICanBoogie
  • Documentation
  • API Reference
  • ActiveRecord 4.0.x
Namespaces
  • ICanBoogie
    • ActiveRecord
      • ActiveRecordCache
      • Driver
      • Property
      • Validate
        • Reader
        • Validator
        • ValidatorProvider
Classes
  • BelongsToRelation
  • Connection
  • ConnectionCollection
  • ConnectionOptions
  • HasManyRelation
  • Model
  • ModelCollection
  • ModelProvider
  • Query
  • Relation
  • RelationCollection
  • RelationNotDefined
  • Schema
  • SchemaColumn
  • Statement
  • Table
Interfaces
  • ActiveRecordCache
  • Driver
  • Exception
Exceptions
  • ActiveRecordClassNotValid
  • ConnectionAlreadyEstablished
  • ConnectionNotDefined
  • ConnectionNotEstablished
  • DriverNotDefined
  • ModelAlreadyInstantiated
  • ModelNotDefined
  • RecordNotFound
  • RecordNotValid
  • ScopeNotDefined
  • StatementInvocationFailed
  • StatementNotValid
  • UnableToSetFetchMode

Class Query

The class offers many features to compose model queries. Most query related methods of the ICanBoogie\ActiveRecord\Model class create a ICanBoogie\ActiveRecord\Query object that is returned for further specification, such as filters or limits.

ICanBoogie\ActiveRecord\Query implements IteratorAggregate uses ICanBoogie\PrototypeTrait (not available)
Namespace: ICanBoogie\ActiveRecord
Located at ActiveRecord/Query.php

Methods summary

protected get_joints( void ) : array

Returns

array
protected get_joints_args( void ) : array

Returns

array
protected get_conditions( void ) : array

Return the conditions collected from ICanBoogie\ActiveRecord\Query::where(), and(), filter_by_*, and scopes.

Return the conditions collected from ICanBoogie\ActiveRecord\Query::where(), and(), filter_by_*, and scopes.

Returns

array
protected get_conditions_args( void ) : array

Return the arguments to the conditions.

Return the arguments to the conditions.

Returns

array
protected get_having_args( void ) : array

Return the arguments to the HAVING clause.

Return the arguments to the HAVING clause.

Returns

array
protected get_model( void ) : ICanBoogie\ActiveRecord\Model

Returns

ICanBoogie\ActiveRecord\Model
protected get_args( void ) : array

Returns the arguments to the query, which include joints arguments, conditions arguments, and having arguments.

Returns the arguments to the query, which include joints arguments, conditions arguments, and having arguments.

Returns

array
public __construct( ICanBoogie\ActiveRecord\Model $model )

Constructor.

Constructor.

Parameters

$model
The model to query.
public __get( $property )

Adds support for model's scopes.

Adds support for model's scopes.

Inheritdoc

public __call( $method, $arguments )

Override the method to handle magic 'filter_by_' methods.

Override the method to handle magic 'filter_by_' methods.

Inheritdoc

public __toString( void ) : string

Convert the query into a string.

Convert the query into a string.

Returns

string
protected render_select( void ) : string

Render the SELECT clause.

Render the SELECT clause.

Returns

string
protected render_from( void ) : string

Render the FROM clause.

Render the FROM clause.

The rendered FROM clause might include some JOINS too.

Returns

string
protected render_joints( void ) : string

Renders the JOIN clauses.

Renders the JOIN clauses.

Returns

string
protected render_main( void ) : string

Render the main body of the query, without the SELECT and FROM clauses.

Render the main body of the query, without the SELECT and FROM clauses.

Returns

string
protected render_order( mixed $order ) : string

Render the ORDER clause.

Render the ORDER clause.

Parameters

$order

Returns

string
protected render_offset_and_limit( integer $offset, integer $limit ) : string

Render the LIMIT and OFFSET clauses.

Render the LIMIT and OFFSET clauses.

Parameters

$offset
$limit

Returns

string
protected resolve_statement( string $statement ) : string

Resolve the placeholders of a statement.

Resolve the placeholders of a statement.

Note: Currently, the method simply forwards the statement to the model's resolve_statement() method.

Parameters

$statement

Returns

string
protected get_model_scope( void ) : array

Return the available scopes for a model class.

Return the available scopes for a model class.

The method uses reflexion to find the scopes, the result is cached.

Returns

array
public select( string $expression ) : ICanBoogie\ActiveRecord\Query

Define the SELECT clause.

Define the SELECT clause.

Parameters

$expression
The expression of the SELECT clause. e.g. 'nid, title'.

Returns

ICanBoogie\ActiveRecord\Query
public join( string|ICanBoogie\ActiveRecord\Query $expression, array $options = [] ) : ICanBoogie\ActiveRecord\Query

Add a JOIN clause.

Add a JOIN clause.

Parameters

$expression

A join can be created from a model reference, another query, or a custom JOIN clause.

  • When $expression is a string starting with : it is considered as a model reference matching the pattern ":" where <model_id> is the identifier of a model that can be retrieved using the model collection associated with the query's model.

  • When $expression is a ICanBoogie\ActiveRecord\Query instance, it is rendered as a string and used as a subquery of the JOIN clause. The $options parameter can be used to customize the output.

  • Otherwise $expression is considered as a raw JOIN clause.

$options

Only used if $expression is a ICanBoogie\ActiveRecord\Query instance. The following options are available: - mode: Join mode. Default: "INNER" - alias: The alias of the subquery. Default: The query's model alias. - on: The column on which to joint is created. Default: The query's model primary key.

<?php

# using a model identifier

$query->join(':nodes');

# using a subquery

$subquery = get_model('updates')
->select('updated_at, $subscriber_id, update_hash')
->order('updated_at DESC')

$query->join($subquery, [ 'on' => 'subscriber_id' ]);

# using a raw clause

$query->join("INNER JOIN `articles` USING(`nid`)");

Returns

ICanBoogie\ActiveRecord\Query
public where( ... $conditions_and_args ) : ICanBoogie\ActiveRecord\Query

Add conditions to the SQL statement.

Add conditions to the SQL statement.

Conditions can either be specified as string or array.

  1. Pure string conditions

If you'de like to add conditions to your statement, you could just specify them in there, just like $model->where('order_count = 2');. This will find all the entries, where the order_count field's value is 2.

  1. Array conditions

Now what if that number could vary, say as an argument from somewhere, or perhaps from the user’s level status somewhere? The find then becomes something like:

$model->where('order_count = ?', 2);

or

$model->where([ 'order_count' => 2 ]);

Or if you want to specify two conditions, you can do it like:

$model->where('order_count = ? AND locked = ?', 2, false);

or

$model->where([ 'order_count' => 2, 'locked' => false ]);

Or if you want to specify subset conditions:

$model->where([ 'order_id' => [ 123, 456, 789 ] ]);

This will return the orders with the order_id 123, 456 or 789.

  1. Modifiers

When using the "identifier" => "value" notation, you can switch the comparison method by prefixing the identifier with a bang "!"

$model->where([ '!order_id' => [ 123, 456, 789 ]]);

This will return the orders with the order_id different than 123, 456 and 789.

$model->where([ '!order_count' => 2 ];

This will return the orders with the order_count different than 2.

Parameters

$conditions_and_args
$conditions_and_args

Returns

ICanBoogie\ActiveRecord\Query
public order( string $order_or_field_name, array $field_values = null ) : ICanBoogie\ActiveRecord\Query

Defines the ORDER clause.

Defines the ORDER clause.

Parameters

$order_or_field_name

The order for the ORDER clause e.g. 'weight, date DESC', or field to order with, in which case $field_values is required.

$field_values
Values of the field specified by $order_or_field_name.

Returns

ICanBoogie\ActiveRecord\Query
public group( string $group ) : ICanBoogie\ActiveRecord\Query

Defines the GROUP clause.

Defines the GROUP clause.

Parameters

$group

Returns

ICanBoogie\ActiveRecord\Query
public having( ... $conditions_and_args ) : ICanBoogie\ActiveRecord\Query

Defines the HAVING clause.

Defines the HAVING clause.

Parameters

$conditions_and_args
$conditions_and_args

Returns

ICanBoogie\ActiveRecord\Query
public offset( $offset ) : ICanBoogie\ActiveRecord\Query

Define the offset of the LIMIT clause.

Define the offset of the LIMIT clause.

Parameters

$offset

Returns

ICanBoogie\ActiveRecord\Query
public limit( integer $limit ) : ICanBoogie\ActiveRecord\Query

Apply the limit and/or offset to the SQL fired.

Apply the limit and/or offset to the SQL fired.

You can use the limit to specify the number of records to be retrieved, ad use the offset to specify the number of records to skip before starting to return records:

$model->limit(10);

Will return a maximum of 10 clients and because ti specifies no offset it will return the first 10 in the table:

$model->limit(5, 10);

Will return a maximum of 10 clients beginning with the 5th.

Parameters

$limit

Returns

ICanBoogie\ActiveRecord\Query
public mode( mixed $mode ) : ICanBoogie\ActiveRecord\Query

Set the fetch mode for the query.

Set the fetch mode for the query.

Parameters

$mode

Returns

ICanBoogie\ActiveRecord\Query

See

http://www.php.net/manual/en/pdostatement.setfetchmode.php
protected prepare( void ) : ICanBoogie\ActiveRecord\Statement

Prepare the query.

Prepare the query.

We use the connection's prepare() method because the statement has already been resolved during the __toString() method and we don't want for the statement to be parsed twice.

Returns

ICanBoogie\ActiveRecord\Statement
protected get_prepared( void ) : ICanBoogie\ActiveRecord\Statement

Return a prepared query.

Return a prepared query.

Returns

ICanBoogie\ActiveRecord\Statement
public query( void ) : ICanBoogie\ActiveRecord\Statement

Prepare and executes the query.

Prepare and executes the query.

Returns

ICanBoogie\ActiveRecord\Statement
public all( ... $mode ) : array

Execute the query and returns an array of records.

Execute the query and returns an array of records.

Parameters

$mode
$mode Fetch mode.

Returns

array
protected get_all( void ) : array

Getter for the ICanBoogie\ActiveRecord\Query::all() magic property.

Getter for the ICanBoogie\ActiveRecord\Query::all() magic property.

Returns

array
public one( ... $mode ) : mixed

Return the first result of the query and close the cursor.

Return the first result of the query and close the cursor.

Parameters

$mode
$mode Fetch node.

Returns

mixed

The return value of this function on success depends on the fetch mode. In all cases, FALSE is returned on failure.

protected get_one( void ) : mixed

Getter for the ICanBoogie\ActiveRecord\Query::one() magic property.

Getter for the ICanBoogie\ActiveRecord\Query::one() magic property.

Returns

mixed

See

ICanBoogie\ActiveRecord\Query::one()
protected get_pairs( void ) : array

Execute que query and return an array of key/value pairs, where the key is the value of the first column and the value of the key the value of the second column.

Execute que query and return an array of key/value pairs, where the key is the value of the first column and the value of the key the value of the second column.

Returns

array
protected get_rc( void ) : string

Return the value of the first column of the first row.

Return the value of the first column of the first row.

Returns

string
public exists( mixed $key = null ) : boolean|array

Check the existence of records in the model.

Check the existence of records in the model.

$model->exists; $model->where('name = "max"')->exists; $model->exists(1); $model->exists(1, 2); $model->exists([ 1, 2 ]);

Parameters

$key

Returns

boolean|array
protected get_exists( void ) : boolean|array

Getter for the ICanBoogie\ActiveRecord\Query::exists() magic property.

Getter for the ICanBoogie\ActiveRecord\Query::exists() magic property.

Returns

boolean|array

See

ICanBoogie\ActiveRecord\Query::exists()
public count( string $column = null ) : integer|array

Implement the 'COUNT' computation.

Implement the 'COUNT' computation.

Parameters

$column
The name of the column to count.

Returns

integer|array
protected get_count( void ) : integer

Getter for the ICanBoogie\ActiveRecord\Query::count() magic property.

Getter for the ICanBoogie\ActiveRecord\Query::count() magic property.

Returns

integer
public average( string $column ) : integer

Implement the 'AVG' computation.

Implement the 'AVG' computation.

Parameters

$column

Returns

integer
public minimum( string $column ) : integer

Implement the 'MIN' computation.

Implement the 'MIN' computation.

Parameters

$column

Returns

integer
public maximum( string $column ) : integer

Implement the 'MAX' computation.

Implement the 'MAX' computation.

Parameters

$column

Returns

integer
public sum( string $column ) : integer

Implement the 'SUM' computation.

Implement the 'SUM' computation.

Parameters

$column

Returns

integer
public delete( string $tables = null ) : boolean

Delete the records matching the conditions and limits of the query.

Delete the records matching the conditions and limits of the query.

Parameters

$tables

When using a JOIN, $tables is used to specify the tables in which records should be deleted. Default: The alias of queried model, only if at least one join clause has been defined using the ICanBoogie\ActiveRecord\Query::join() method.

Returns

boolean
The result of the operation.

Todo-20140901:

reflect on join to add the required tables by default, discarding tables joined with the LEFT mode.


public getIterator( void )

Return an iterator for the query.

Return an iterator for the query.

Implementation of

IteratorAggregate::getIterator()

Magic methods summary

public and( $conditions, $conditions_args = null, $_ = null) Alias to {@link where( ) : ICanBoogie\ActiveRecord\Query

}.

}.

Parameters

$conditions
$conditions_args
$_

Returns

ICanBoogie\ActiveRecord\Query

Constants summary

LIMIT_MAX : string
'18446744073709551615'

Properties summary

protected $select : string

Part of the SELECT clause.

Part of the SELECT clause.


		
protected $joints : array

JOIN clauses.

JOIN clauses.

[]
protected $joints_args : array

Joints arguments.

Joints arguments.

[]
protected $conditions : array

The conditions collected from ICanBoogie\ActiveRecord\Query::where(), and(), filter_by_*, and scopes.

The conditions collected from ICanBoogie\ActiveRecord\Query::where(), and(), filter_by_*, and scopes.

[]
protected $conditions_args : array

Arguments for the conditions.

Arguments for the conditions.

[]
protected $group : string

Part of the GROUP BY clause.

Part of the GROUP BY clause.


		
protected $order : mixed

Part of the ORDER BY clause.

Part of the ORDER BY clause.


		
protected $having : string

Part of the HAVING clause.

Part of the HAVING clause.


		
protected $having_args : array

Arguments to the HAVING clause.

Arguments to the HAVING clause.

[]
protected $offset : integer

The number of records the skip before fetching.

The number of records the skip before fetching.


		
protected $limit : integer

The maximum number of records to fetch.

The maximum number of records to fetch.


		
protected $mode : mixed

Fetch mode.

Fetch mode.


		
protected $model : ICanBoogie\ActiveRecord\Model

The target model of the query.

The target model of the query.


		
protected static $scopes_by_classes : array

Cache available scopes by model class.

Cache available scopes by model class.

[]

Magic properties

public read-only $all : array

An array with all the records matching the query.

public read-only $one : mixed

The first record matching the query.

public read-only $pairs : array

An array of key/value pairs.

public read-only $rc : array

The first column of the first row matching the query.

public read-only $count : integer

The number of records matching the query.

public read-only $exists : boolean|array

true if a record matching the query exists, false otherwise. If there is multiple records, the property is an array of booleans.

public read-only $model : ICanBoogie\ActiveRecord\Model

The target model of the query.

public read-only $joints : array

The joints collection from ICanBoogie\ActiveRecord\Query::join().

public read-only $joints_args : array

The arguments to the joints.

public read-only $conditions : array

The conditions collected from ICanBoogie\ActiveRecord\Query::where(), and(), filter_by_*, and scopes.

public read-only $conditions_args : array

The arguments to the conditions.

public read-only $having_args : array

The arguments to the HAVING clause.

public read-only $args : array

Returns the arguments to the query.

public read-only $prepared : ICanBoogie\ActiveRecord\Query

Return a prepared query.

ActiveRecord 4.0.x – Check on GitHub – API documentation generated by ApiGen