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'}}}
Afficher le fichier
Open project: unionofrad/lithium
Class Usage Examples
Protected Properties
Свойство |
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. |
|
Méthodes publiques
Méthode |
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. |
|
Méthodes protégées
Method Details
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. |
Résultat |
mixed |
Returns the value of the field specified in `$name`, and wraps complex data
types in sub-`Document` objects. |
public __isset ( $name ) : boolean |
$name |
|
The field name, as specified with an object property. |
Résultat |
boolean |
True if the field specified in `$name` exists, false otherwise. |
$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'`. |
Résultat |
void |
|
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();
_getNested()
protected méthode
_init()
protected méthode
_relation()
protected méthode
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. |
Résultat |
object |
Returns a new `Document` object instance. |
_setNested()
protected 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 ($_handle). If no more records can be fetched, returns null.
public next ( ) : mixed |
Résultat |
mixed |
Returns the next record in the set, or `null`, if no more records are
available. |
offsetExists()
public méthode
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. |
Résultat |
boolean |
Returns `true` if `$offset` is a field in the document, otherwise `false`. |
offsetGet()
public méthode
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. |
Résultat |
mixed |
Returns either a sub-object in the document, or a scalar field value. |
offsetSet()
public méthode
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. |
Résultat |
void |
|
offsetUnset()
public méthode
Allows document fields to be unset as array keys, i.e. unset($document['_id']).
Rewinds to the first item.
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 |
|
Résultat |
void |
|
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`. |
Résultat |
void |
|
Adds conversions checks to ensure certain class types and embedded values are properly cast.
Used by the Iterator interface to determine the current state of the iteration, and when
to stop iterating.
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.
protected string $_pathKey |
Résultat |
string |
|
$_removed protected_oe property
Removed keys list. Contains names of the fields will be removed from the backend data store
protected array $_removed |
Résultat |
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.
protected array $_stats |
Résultat |
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 |
Résultat |
boolean |
|