ICanBoogie/ActiveRecord master
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • ActiveRecord
      • Driver

Classes

  • ActiveRecordCacheBase
  • BelongsToRelation
  • Connection
  • ConnectionCollection
  • ConnectionOptions
  • DateTimePropertySupport
  • HasManyRelation
  • Helpers
  • Model
  • ModelCollection
  • Query
  • Relation
  • RelationCollection
  • RelationNotDefined
  • RuntimeActiveRecordCache
  • Schema
  • SchemaColumn
  • Statement
  • Table

Interfaces

  • ActiveRecordCache
  • Driver
  • Exception

Traits

  • CreatedAtProperty
  • DateProperty
  • DateTimeProperty
  • FinishAtProperty
  • FinishedAtProperty
  • StartAtProperty
  • StartedAtProperty
  • UpdatedAtProperty

Exceptions

  • ActiveRecordClassNotValid
  • ConnectionAlreadyEstablished
  • ConnectionNotDefined
  • ConnectionNotEstablished
  • DriverNotDefined
  • ModelAlreadyInstantiated
  • ModelNotDefined
  • RecordNotFound
  • ScopeNotDefined
  • StatementInvocationFailed
  • StatementNotValid
  • UnableToSetFetchMode

Functions

  • extract_charset_and_collate
  • get_model

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 array
# get_joints( )

Returns

array
protected array
# get_joints_args( )

Returns

array
protected array
# get_conditions( )

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 array
# get_conditions_args( )

Return the arguments to the conditions.

Return the arguments to the conditions.

Returns

array
protected array
# get_having_args( )

Return the arguments to the HAVING clause.

Return the arguments to the HAVING clause.

Returns

array
protected ICanBoogie\ActiveRecord\Model
# get_model( )

Returns

ICanBoogie\ActiveRecord\Model
protected array
# get_args( )

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 string
# __toString( )

Convert the query into a string.

Convert the query into a string.

Returns

string
protected string
# render_select( )

Render the SELECT clause.

Render the SELECT clause.

Returns

string
protected string
# render_from( )

Render the FROM clause.

Render the FROM clause.

The rendered FROM clause might include some JOINS too.

Returns

string
protected string
# render_joints( )

Renders the JOIN clauses.

Renders the JOIN clauses.

Returns

string
protected string
# render_main( )

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 string
# render_order( mixed $order )

Render the ORDER clause.

Render the ORDER clause.

Parameters

$order

Returns

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

Render the LIMIT and OFFSET clauses.

Render the LIMIT and OFFSET clauses.

Parameters

$offset
$limit

Returns

string
protected string
# resolve_statement( string $statement )

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 array
# get_model_scope( )

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 ICanBoogie\ActiveRecord\Query
# select( string $expression )

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 ICanBoogie\ActiveRecord\Query
# join( string|ICanBoogie\ActiveRecord\Query $expression, array $options = [] )

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 ICanBoogie\ActiveRecord\Query
# where( mixed $conditions, mixed $conditions_args = null, mixed $_ = null )

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
$conditions_args
$_
[optional]

Returns

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

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 ICanBoogie\ActiveRecord\Query
# group( string $group )

Defines the GROUP clause.

Defines the GROUP clause.

Parameters

$group

Returns

ICanBoogie\ActiveRecord\Query
public ICanBoogie\ActiveRecord\Query
# having( mixed $conditions, mixed $conditions_args = null )

Defines the HAVING clause.

Defines the HAVING clause.

Parameters

$conditions
$conditions_args

Returns

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

Define the offset of the LIMIT clause.

Define the offset of the LIMIT clause.

Parameters

$offset

Returns

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

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 ICanBoogie\ActiveRecord\Query
# mode( mixed $mode )

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 ICanBoogie\ActiveRecord\Statement
# prepare( )

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 ICanBoogie\ActiveRecord\Statement
# get_prepared( )

Return a prepared query.

Return a prepared query.

Returns

ICanBoogie\ActiveRecord\Statement
public ICanBoogie\ActiveRecord\Statement
# query( )

Prepare and executes the query.

Prepare and executes the query.

Returns

ICanBoogie\ActiveRecord\Statement
public array
# all( mixed $_ = null )

Execute the query and returns an array of records.

Execute the query and returns an array of records.

Parameters

$_
[optional]

Returns

array
protected array
# get_all( )

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

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

Returns

array
public mixed
# one( $_ = null )

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

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

Parameters

$_
Fetch mode.

Returns

mixed

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

protected mixed
# get_one( )

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 array
# get_pairs( )

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 string
# get_rc( )

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 boolean|array
# exists( mixed $key = null )

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 boolean|array
# get_exists( )

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 integer|array
# count( string $column = null )

Implement the 'COUNT' computation.

Implement the 'COUNT' computation.

Parameters

$column
The name of the column to count.

Returns

integer|array
protected integer
# get_count( )

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

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

Returns

integer
public integer
# average( string $column )

Implement the 'AVG' computation.

Implement the 'AVG' computation.

Parameters

$column

Returns

integer
public integer
# minimum( string $column )

Implement the 'MIN' computation.

Implement the 'MIN' computation.

Parameters

$column

Returns

integer
public integer
# maximum( string $column )

Implement the 'MAX' computation.

Implement the 'MAX' computation.

Parameters

$column

Returns

integer
public integer
# sum( string $column )

Implement the 'SUM' computation.

Implement the 'SUM' computation.

Parameters

$column

Returns

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

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( )

Return an iterator for the query.

Return an iterator for the query.

Implementation of

IteratorAggregate::getIterator()

Magic methods summary

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

}.

}.

Parameters

$conditions
$conditions_args
$_

Returns

ICanBoogie\ActiveRecord\Query

Constants summary

string LIMIT_MAX
# '18446744073709551615'

Properties summary

protected string $select

Part of the SELECT clause.

Part of the SELECT clause.

#
protected array $joints

JOIN clauses.

JOIN clauses.

# []
protected array $joints_args

Joints arguments.

Joints arguments.

# []
protected array $conditions

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 array $conditions_args

Arguments for the conditions.

Arguments for the conditions.

# []
protected string $group

Part of the GROUP BY clause.

Part of the GROUP BY clause.

#
protected mixed $order

Part of the ORDER BY clause.

Part of the ORDER BY clause.

#
protected string $having

Part of the HAVING clause.

Part of the HAVING clause.

#
protected array $having_args

Arguments to the HAVING clause.

Arguments to the HAVING clause.

# []
protected integer $offset

The number of records the skip before fetching.

The number of records the skip before fetching.

#
protected integer $limit

The maximum number of records to fetch.

The maximum number of records to fetch.

#
protected mixed $mode

Fetch mode.

Fetch mode.

#
protected ICanBoogie\ActiveRecord\Model $model

The target model of the query.

The target model of the query.

#
protected static array $scopes_by_classes

Cache available scopes by model class.

Cache available scopes by model class.

# []

Magic properties

public read-only array $all

An array with all the records matching the query.

public read-only mixed $one

The first record matching the query.

public read-only array $pairs

An array of key/value pairs.

public read-only array $rc

The first column of the first row matching the query.

public read-only integer $count

The number of records matching the query.

public read-only boolean|array $exists

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 ICanBoogie\ActiveRecord\Model $model

The target model of the query.

public read-only array $joints

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

public read-only array $joints_args

The arguments to the joints.

public read-only array $conditions

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

public read-only array $conditions_args

The arguments to the conditions.

public read-only array $having_args

The arguments to the HAVING clause.

public read-only array $args

Returns the arguments to the query.

public read-only ICanBoogie\ActiveRecord\Query $prepared

Return a prepared query.

ICanBoogie/ActiveRecord master API documentation generated by ApiGen