PHP Class lithium\data\entity\Document

A Document object's fields can represent a collection of both simple and complex data types, as well as other Document objects. Given the following data (document) structure: {{{ { _id: 12345. name: 'Acme, Inc.', employees: { 'Larry': { email: '[email protected]' }, 'Curly': { email: '[email protected]' }, 'Moe': { email: '[email protected]' } } } }}} You can query the object as follows: {{{$acme = Company::find(12345);}}} This returns a Document object, populated with the raw representation of the data. {{{print_r($acme->to('array')); Yields: array( '_id' => 12345, 'name' => 'Acme, Inc.', 'employees' => array( 'Larry' => array('email' => '[email protected]'), 'Curly' => array('email' => '[email protected]'), 'Moe' => array('email' => '[email protected]') ) )}}} As with other database objects, a Document exposes its fields as object properties, like so: {{{echo $acme->name; // echoes 'Acme, Inc.'}}} However, accessing a field containing a data set will return that data set wrapped in a sub-Document object., i.e.: {{{$employees = $acme->employees; returns a Document object with the data in 'employees'}}}
Inheritance: extends lithium\data\Entity, implements Iterator, implements ArrayAccess
ファイルを表示 Open project: unionofrad/lithium Class Usage Examples

Protected Properties

Property Type Description
$_pathKey string If this Document instance has a parent document (see $_parent), this value indicates the key name of the parent document that contains it.
$_removed array Removed keys list. Contains names of the fields will be removed from the backend data store
$_stats array Contains an array of backend-specific statistics generated by the query that produced this Document object. These stats are accessible via the stats() method.
$_valid boolean Holds the current iteration state. Used by Document::valid() to terminate foreach loops when there are no more fields to iterate over.

Public Methods

Method Description
__get ( $name ) : mixed PHP magic method used when accessing fields as document properties, i.e. $document->_id.
__isset ( $name ) : boolean PHP magic method used to check the presence of a field as document properties, i.e.
__set ( $name, $value = null ) : void PHP magic method used when setting properties on the Document instance, i.e.
__unset ( string $name ) : void PHP magic method used when unset() is called on a Document instance.
current ( )
export ( array $options = [] )
key ( )
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 ($_handle). If no more records can be fetched, returns null.
offsetExists ( mixed $offset ) : boolean Allows document fields to be tested as array keys, i.e. isset($document['_id']).
offsetGet ( mixed $offset ) : mixed Allows document fields to be accessed as array keys, i.e. $document['_id'].
offsetSet ( mixed $offset, mixed $value ) : void Allows document fields to be assigned as array keys, i.e. $document['_id'] = $id.
offsetUnset ( string $key ) : void Allows document fields to be unset as array keys, i.e. unset($document['_id']).
rewind ( ) : mixed Rewinds to the first item.
set ( array $data, array $options = [] ) : void Allows several properties to be assigned at once.
sync ( mixed $id = null, array $data = [], array $options = [] ) : void Extends the parent implementation to ensure that child documents are properly synced as well.
to ( string $format, array $options = [] ) : mixed Adds conversions checks to ensure certain class types and embedded values are properly cast.
valid ( ) : boolean Used by the Iterator interface to determine the current state of the iteration, and when to stop iterating.

Protected Methods

Method Description
_getNested ( $name )
_init ( )
_relation ( string $classType, string $key, array $data, array $options = [] ) : object Instantiates a new Document object as a descendant of the current object, and sets all default values and internal state.
_setNested ( $name, $value )

Method Details

__get() public method

PHP magic method used when accessing fields as document properties, i.e. $document->_id.
public __get ( $name ) : mixed
$name The field name, as specified with an object property.
return mixed Returns the value of the field specified in `$name`, and wraps complex data types in sub-`Document` objects.

__isset() public method

$document->_id.
public __isset ( $name ) : boolean
$name The field name, as specified with an object property.
return boolean True if the field specified in `$name` exists, false otherwise.

__set() public method

$document->title = 'Lorem Ipsum'. If $value is a complex data type (i.e. associative array), it is wrapped in a sub-Document object before being appended.
public __set ( $name, $value = null ) : void
$name The name of the field/property to write to, i.e. `title` in the above example.
$value The value to write, i.e. `'Lorem Ipsum'`.
return void

__unset() public method

Use case for this would be when you wish to edit a document and remove a field, ie.: $doc = Post::find($id); unset($doc->fieldName); $doc->save();
public __unset ( string $name ) : void
$name string The name of the field to remove.
return void

_getNested() protected method

protected _getNested ( $name )

_init() protected method

protected _init ( )

_relation() protected method

Instantiates a new Document object as a descendant of the current object, and sets all default values and internal state.
protected _relation ( string $classType, string $key, array $data, array $options = [] ) : object
$classType string The type of class to create, either `'entity'` or `'set'`.
$key string The key name to which the related object is assigned.
$data array The internal data of the related object.
$options array Any other options to pass when instantiating the related object.
return object Returns a new `Document` object instance.

_setNested() protected method

protected _setNested ( $name, $value )

current() public method

public current ( )

export() public method

public export ( array $options = [] )
$options array

key() public method

public key ( )

next() public method

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 ($_handle). If no more records can be fetched, returns null.
public next ( ) : mixed
return mixed Returns the next record in the set, or `null`, if no more records are available.

offsetExists() public method

Allows document fields to be tested as array keys, i.e. isset($document['_id']).
public offsetExists ( mixed $offset ) : boolean
$offset mixed String or integer indicating the offset or the name of a field in an individual document.
return boolean Returns `true` if `$offset` is a field in the document, otherwise `false`.

offsetGet() public method

Allows document fields to be accessed as array keys, i.e. $document['_id'].
public offsetGet ( mixed $offset ) : mixed
$offset mixed String or integer indicating the offset or index of a document in a set, or the name of a field in an individual document.
return mixed Returns either a sub-object in the document, or a scalar field value.

offsetSet() public method

Allows document fields to be assigned as array keys, i.e. $document['_id'] = $id.
public offsetSet ( mixed $offset, mixed $value ) : void
$offset mixed String or integer indicating the offset or the name of a field in an individual document.
$value mixed The value to assign to the field.
return void

offsetUnset() public method

Allows document fields to be unset as array keys, i.e. unset($document['_id']).
public offsetUnset ( string $key ) : void
$key string The name of a field in an individual document.
return void

rewind() public method

Rewinds to the first item.
public rewind ( ) : mixed
return mixed The current item after rewinding.

set() public method

For example: $doc->set(array('title' => 'Lorem Ipsum', 'value' => 42));
public set ( array $data, array $options = [] ) : void
$data array An associative array of fields and values to assign to the `Document`.
$options array
return void

sync() public method

Extends the parent implementation to ensure that child documents are properly synced as well.
public sync ( mixed $id = null, array $data = [], array $options = [] ) : void
$id mixed
$data array
$options array Options when calling this method: - `'recursive'` _boolean_: If `true` attempts to sync nested objects as well. Otherwise, only syncs the current object. Defaults to `true`.
return void

to() public method

Adds conversions checks to ensure certain class types and embedded values are properly cast.
public to ( string $format, array $options = [] ) : mixed
$format string Currently only `array` is supported.
$options array
return mixed

valid() public method

Used by the Iterator interface to determine the current state of the iteration, and when to stop iterating.
public valid ( ) : boolean
return boolean

Property Details

$_pathKey protected_oe property

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

$_removed protected_oe property

Removed keys list. Contains names of the fields will be removed from the backend data store
protected array $_removed
return array

$_stats protected_oe property

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

$_valid protected_oe property

Holds the current iteration state. Used by Document::valid() to terminate foreach loops when there are no more fields to iterate over.
protected bool $_valid
return boolean