PHP Class lithium\data\Collection

Inheritance: extends lithium\util\Collection
Afficher le fichier Open project: unionofrad/lithium Class Usage Examples

Protected Properties

Свойство Type Description
$_autoConfig array Holds an array of values that should be processed on initialization.
$_exists boolean Indicates whether this array was part of a document loaded from a data source, or is part of a new document, or is in newly-added field of an existing document.
$_handlers array Hold the "data export" handlers where the keys are fully-namespaced class names, and the values are closures that take an instance of the class as a parameter, and return an array or scalar value that the instance represents.
$_model string The fully-namespaced class name of the model object to which this entity set is bound. This is usually the model that executed the query which created this object.
$_parent object A reference to this object's parent Document object.
$_pathKey string If this Collection instance has a parent document (see $_parent), this value indicates the key name of the parent document that contains it.
$_query object A reference to the query object that originated this entity set; usually an instance of lithium\data\model\Query.
$_result resource | object A pointer or resource that is used to load entities from the backend data source that originated this collection.
$_schema lithium\data\Schema If the Collection has a schema object assigned (rather than loading one from a model), it will be assigned here.
$_started boolean Set to true when the collection has begun iterating.
$_stats array Contains an array of backend-specific statistics generated by the query that produced this Collection object. These stats are accessible via the stats() method.
$_valid boolean Indicates whether the current position is valid or not. This overrides the default value of the parent class.

Méthodes publiques

Méthode Description
__construct ( array $config = [] ) : void Constructor.
__destruct ( ) : void Destructor. Ensures that the data set's connection is closed when the object is destroyed.
assignTo ( object $parent, array $config = [] ) : void Configures protected properties of a Collection so that it is parented to $parent.
close ( ) Executes when the associated result resource pointer reaches the end of its data set. The resource is freed by the connection, and the reference to the connection is unlinked.
closed ( ) : boolean Checks to see if this entity has already fetched all available entities and freed the associated result resource.
current ( ) : object | boolean Returns the currently pointed to record in the set.
data ( ) : array Converts the current state of the data structure to an array.
each ( callback $filter ) : object Applies a callback to all data in the collection.
exists ( ) : boolean A flag indicating whether or not the items of this collection exists.
find ( mixed $filter, array $options = [] ) : mixed Overrides parent find() implementation to enable key/value-based filtering of entity objects contained in this collection.
first ( mixed $filter = null ) : object Overrides parent first() implementation to enable key/value-based filtering.
key ( boolean $full = false ) : mixed Returns the currently pointed to record's unique key.
keys ( ) : array Returns the item keys.
map ( callback $filter, array $options = [] ) : object Applies a callback to a copy of all data in the collection and returns the result.
meta ( ) : array Returns meta information for this Collection.
model ( ) : string Returns the model which this particular collection is based off of.
next ( ) : mixed Returns the next document in the set, and advances the object's internal pointer. If the end of the set is reached, a new document will be fetched from the data source connection handle If no more documents can be fetched, returns null.
offsetExists ( string $offset ) : boolean Returns a boolean indicating whether an offset exists for the current Collection.
offsetGet ( mixed $offset ) : mixed Gets an Entity object using PHP's array syntax, i.e. $documents[3] or $records[5].
offsetSet ( string $offset, mixed $data ) : mixed Adds the specified object to the Collection instance, and assigns associated metadata to the added object.
offsetUnset ( integer $offset ) Unsets an offset.
parent ( ) : object Returns the object's parent Document object.
reduce ( callback $filter, mixed $initial = false ) : mixed Reduce, or fold, a collection down to a single value
result ( ) : object Return's the pointer or resource that is used to load entities from the backend data source that originated this collection. This is useful in many cases for additional methods related to debugging queries.
rewind ( ) Rewinds the collection to the beginning.
schema ( $field = null )
serialize ( ) : string Prepares, enables and executes serialization of the object.
set ( arary $values ) : void Allows several items to be assigned at once.
sort ( string | callable $field = 'id', array $options = [] ) : lithium\data\Collection Sorts the objects in the collection, useful in situations where you are already using the underlying datastore to sort results.
stats ( string $name = null ) : mixed Gets the stat or stats associated with this Collection.
to ( string $format, array $options = [] ) : mixed Converts a Collection object to another type of object, or a simple type such as an array.
unserialize ( string $data ) : void Prepares, enables and executes unserialization of the object.
valid ( ) : boolean Checks if current position is valid.

Méthodes protégées

Méthode Description
_filterFromArray ( array $filter ) : Closure Creates a filter based on an array of key/value pairs that must match the items in a Collection.
_init ( )
_populate ( ) : mixed A method to be implemented by concrete Collection classes which, provided a reference to a backend data source, and a resource representing a query result cursor, fetches new result data and wraps it in the appropriate object type, which is added into the Collection and returned.
_set ( mixed $data = null, mixed $offset = null, array $options = [] ) : object A method to be implemented by concrete Collection classes which sets data to a specified offset and wraps all data array in its appropriate object type.

Method Details

__construct() public méthode

Constructor.
See also: lithium\data\Collection::$_data
See also: lithium\data\Collection::$_model
public __construct ( array $config = [] ) : void
$config array Available configuration options are: - `'data'` _array_ - `'model'` _string|null_
Résultat void

__destruct() public méthode

Destructor. Ensures that the data set's connection is closed when the object is destroyed.
public __destruct ( ) : void
Résultat void

_filterFromArray() protected méthode

Creates a filter based on an array of key/value pairs that must match the items in a Collection.
protected _filterFromArray ( array $filter ) : Closure
$filter array An array of key/value pairs used to filter `Collection` items.
Résultat Closure Returns a closure that wraps the array and attempts to match each value against `Collection` item properties.

_init() protected méthode

protected _init ( )

_populate() abstract protected méthode

A method to be implemented by concrete Collection classes which, provided a reference to a backend data source, and a resource representing a query result cursor, fetches new result data and wraps it in the appropriate object type, which is added into the Collection and returned.
abstract protected _populate ( ) : mixed
Résultat mixed Returns the next `Record`, `Document` object or other `Entity` object if exists. Returns `null` otherwise.

_set() abstract protected méthode

A method to be implemented by concrete Collection classes which sets data to a specified offset and wraps all data array in its appropriate object type.
See also: lithium\data\Collection::_populate()
See also: lithium\data\Collection::offsetSet()
abstract protected _set ( mixed $data = null, mixed $offset = null, array $options = [] ) : object
$data mixed An array or an `Entity` object to set.
$offset mixed The offset. If offset is `null` data is simply appended to the set.
$options array Any additional options to pass to the `Entity`'s constructor.
Résultat object Returns the inserted `Record`, `Document` object or other `Entity` object.

assignTo() public méthode

Configures protected properties of a Collection so that it is parented to $parent.
public assignTo ( object $parent, array $config = [] ) : void
$parent object
$config array
Résultat void

close() public méthode

Executes when the associated result resource pointer reaches the end of its data set. The resource is freed by the connection, and the reference to the connection is unlinked.
public close ( )

closed() public méthode

Checks to see if this entity has already fetched all available entities and freed the associated result resource.
public closed ( ) : boolean
Résultat boolean Returns true if all entities are loaded and the database resources have been freed, otherwise returns false.

current() public méthode

Returns the currently pointed to record in the set.
public current ( ) : object | boolean
Résultat object | boolean An instance of `Record` or `false` if there is no current valid one.

data() public méthode

Converts the current state of the data structure to an array.
public data ( ) : array
Résultat array Returns the array value of the data in this `Collection`.

each() public méthode

Overridden to load any data that has not yet been loaded.
public each ( callback $filter ) : object
$filter callback The filter to apply.
Résultat object This collection instance.

exists() public méthode

A flag indicating whether or not the items of this collection exists.
public exists ( ) : boolean
Résultat boolean `True` if exists, `false` otherwise.

find() public méthode

Overrides parent find() implementation to enable key/value-based filtering of entity objects contained in this collection.
public find ( mixed $filter, array $options = [] ) : mixed
$filter mixed Callback to use for filtering, or array of key/value pairs which entity properties will be matched against.
$options array Options to modify the behavior of this method. See the documentation for the `$options` parameter of `lithium\util\Collection::find()`.
Résultat mixed The filtered items. Will be an array unless `'collect'` is defined in the `$options` argument, then an instance of this class will be returned.

first() public méthode

Overrides parent first() implementation to enable key/value-based filtering.
public first ( mixed $filter = null ) : object
$filter mixed In addition to a callback (see parent), can also be an array where the keys and values must match the property values of the objects being inspected.
Résultat object Returns the first object found matching the filter criteria.

key() public méthode

Returns the currently pointed to record's unique key.
public key ( boolean $full = false ) : mixed
$full boolean If true, returns the complete key.
Résultat mixed

keys() public méthode

Returns the item keys.
public keys ( ) : array
Résultat array The keys of the items.

map() public méthode

Overriden to load any data that has not yet been loaded.
public map ( callback $filter, array $options = [] ) : object
$filter callback The filter to apply.
$options array The available options are: - `'collect'`: If `true`, the results will be returned wrapped in a new `Collection` object or subclass.
Résultat object The filtered data.

meta() public méthode

Returns meta information for this Collection.
public meta ( ) : array
Résultat array

model() public méthode

Returns the model which this particular collection is based off of.
public model ( ) : string
Résultat string The fully qualified model class name.

next() public méthode

Returns the next document in the set, and advances the object's internal pointer. If the end of the set is reached, a new document will be fetched from the data source connection handle If no more documents can be fetched, returns null.
public next ( ) : mixed
Résultat mixed Returns the next document in the set, or `false`, if no more documents are available.

offsetExists() public méthode

Returns a boolean indicating whether an offset exists for the current Collection.
public offsetExists ( string $offset ) : boolean
$offset string String or integer indicating the offset or index of an entity in the set.
Résultat boolean Result.

offsetGet() public méthode

Gets an Entity object using PHP's array syntax, i.e. $documents[3] or $records[5].
public offsetGet ( mixed $offset ) : mixed
$offset mixed The offset.
Résultat mixed Returns an `Entity` object if exists otherwise returns `null`.

offsetSet() public méthode

Adds the specified object to the Collection instance, and assigns associated metadata to the added object.
public offsetSet ( string $offset, mixed $data ) : mixed
$offset string The offset to assign the value to.
$data mixed The entity object to add.
Résultat mixed Returns the set `Entity` object.

offsetUnset() public méthode

Unsets an offset.
public offsetUnset ( integer $offset )
$offset integer The offset to unset.

parent() public méthode

Returns the object's parent Document object.
public parent ( ) : object
Résultat object

reduce() public méthode

Overridden to load any data that has not yet been loaded.
public reduce ( callback $filter, mixed $initial = false ) : mixed
$filter callback The filter to apply.
$initial mixed Initial value
Résultat mixed A single reduced value

result() public méthode

Return's the pointer or resource that is used to load entities from the backend data source that originated this collection. This is useful in many cases for additional methods related to debugging queries.
public result ( ) : object
Résultat object The pointer or resource from the data source

rewind() public méthode

Rewinds the collection to the beginning.
public rewind ( )

schema() public méthode

public schema ( $field = null )

serialize() public méthode

Note: because of the limitations outlined below custom handlers are ignored with serialized objects. Pulls all results to entirely populate _data and closes the object freeing the associated result resource. This allows for skipping the _result property which may hold unserializable PDOStatements. Properties that hold anonymous functions are also skipped. Some of these can almost be reconstructed (_handlers) others cannot (_methodFilters).
public serialize ( ) : string
Résultat string Serialized properties of the object.

set() public méthode

For example: $collection->set(array( 42 => array('title' => 'Lorem Ipsum'), 43 => array('title' => 'Dolor Amet') ));
public set ( arary $values ) : void
$values arary An associative array of fields and values to assign to the `Collection`.
Résultat void

sort() public méthode

Overriden to load any data that has not yet been loaded.
See also: lithium\util\Collection::sort()
public sort ( string | callable $field = 'id', array $options = [] ) : lithium\data\Collection
$field string | callable The field to sort the data on, can also be a callback to a custom sort function.
$options array Reserved for future use.
Résultat lithium\data\Collection Returns itself.

stats() public méthode

Gets the stat or stats associated with this Collection.
public stats ( string $name = null ) : mixed
$name string Stat name.
Résultat mixed Single stat if `$name` supplied, else all stats for this `Collection`.

to() public méthode

The supported values of $format depend on the format handlers registered in the static property Collection::$_formats. The Collection class comes with built-in support for array conversion, but other formats may be registered. Once the appropriate handlers are registered, a Collection instance can be converted into any handler-supported format, i.e.: $collection->to('json'); // returns a JSON string $collection->to('xml'); // returns an XML string _Please note that Lithium does not ship with a default XML handler, but one can be configured easily._
See also: lithium\util\Collection::toArray()
See also: lithium\util\Collection::formats()
See also: lithium\util\Collection::$_formats
public to ( string $format, array $options = [] ) : mixed
$format string By default the only supported value is `'array'`. However, additional format handlers can be registered using the `formats()` method.
$options array Options for converting this collection: - `'internal'` _boolean_: Indicates whether the current internal representation of the collection should be exported. Defaults to `false`, which uses the standard iterator interfaces. This is useful for exporting record sets, where records are lazy-loaded, and the collection must be iterated in order to fetch all objects. - `'indexed'` _boolean|null_: Allows to control how converted data is keyed. When set to `true` will force indexed conversion of the collection (the default) even if the collection has a parent. When `false` will convert without indexing. Provide `null` as a value to this option to only index when the collection has no parent.
Résultat mixed The object converted to the value specified in `$format`; usually an array or string.

unserialize() public méthode

Restores state of the object including pulled results. Tries to restore _handlers by calling into _init().
public unserialize ( string $data ) : void
$data string Serialized properties of the object.
Résultat void

valid() public méthode

Checks if current position is valid.
public valid ( ) : boolean
Résultat boolean `true` if valid, `false` otherwise.

Property Details

$_autoConfig protected_oe property

Holds an array of values that should be processed on initialization.
protected array $_autoConfig
Résultat array

$_exists protected_oe property

Indicates whether this array was part of a document loaded from a data source, or is part of a new document, or is in newly-added field of an existing document.
protected bool $_exists
Résultat boolean

$_handlers protected_oe property

Hold the "data export" handlers where the keys are fully-namespaced class names, and the values are closures that take an instance of the class as a parameter, and return an array or scalar value that the instance represents.
See also: lithium\data\Collection::to()
protected array $_handlers
Résultat array

$_model protected_oe property

The fully-namespaced class name of the model object to which this entity set is bound. This is usually the model that executed the query which created this object.
protected string $_model
Résultat string

$_parent protected_oe property

A reference to this object's parent Document object.
protected object $_parent
Résultat object

$_pathKey protected_oe property

If this Collection instance has a parent document (see $_parent), this value indicates the key name of the parent document that contains it.
See also: lithium\data\Collection::$_parent
protected string $_pathKey
Résultat string

$_query protected_oe property

A reference to the query object that originated this entity set; usually an instance of lithium\data\model\Query.
See also: lithium\data\model\Query
protected object $_query
Résultat object

$_result protected_oe property

A pointer or resource that is used to load entities from the backend data source that originated this collection.
protected resource|object $_result
Résultat resource | object

$_schema protected_oe property

If the Collection has a schema object assigned (rather than loading one from a model), it will be assigned here.
See also: lithium\data\Schema
protected lithium\data\Schema $_schema
Résultat lithium\data\Schema

$_started protected_oe property

Set to true when the collection has begun iterating.
protected bool $_started
Résultat boolean

$_stats protected_oe property

Contains an array of backend-specific statistics generated by the query that produced this Collection object. These stats are accessible via the stats() method.
See also: lithium\data\Collection::stats()
protected array $_stats
Résultat array

$_valid protected_oe property

Indicates whether the current position is valid or not. This overrides the default value of the parent class.
See also: lithium\util\Collection::valid()
protected bool $_valid
Résultat boolean