Свойство | Type | Description | |
---|---|---|---|
$belongsTo | array | Model belongsTo relations. | |
$hasMany | array | Model hasMany relations. | |
$hasOne | array | Model hasOne relations. | |
$validates | array | Example usage: public $validates = array( 'title' => 'please enter a title', 'email' => array( array('notEmpty', 'message' => 'Email is empty.'), array('email', 'message' => 'Email is not valid.'), ) ); |
Свойство | Type | Description | |
---|---|---|---|
$_autoConfig | array | Holds an array of values that should be processed on Model::config(). Each value should have a matching protected property (prefixed with _) defined in the class. If the property is an array, the property name should be the key and the value should be 'merge'. | |
$_classes | array | Class dependencies. | |
$_finders | array | Custom find query properties, indexed by name. | |
$_inherits | array | Holds an array of attributes to be inherited. | |
$_initialized | array | List of initialized instances. | |
$_initializers | array | Array of closures used to lazily initialize metadata. | |
$_instanceFilters | array | Stores the filters that are applied to the model instances stored in Model::$_instances. | |
$_instanceMethods | array | Stores all custom instance methods created by Model::instanceMethods. | |
$_instances | array | While the Model public API does not require instantiation thanks to late static binding introduced in PHP 5.3, LSB does not apply to class attributes. In order to prevent you from needing to redeclare every single Model class attribute in subclasses, instances of the models are stored and used internally. | |
$_meta | array | - connection: The name of the connection (as defined in Connections::add()) to which the model should bind - key: The primary key or identifier key for records / documents this model produces, i.e. 'id' or array('_id', '_rev'). Defaults to 'id'. - name: The canonical name of this model. Defaults to the class name. - source: The name of the database table or document collection to bind to. Defaults to the lower-cased and underscored name of the class, i.e. class UserProfile maps to 'user_profiles'. - title: The field or key used as the title for each record. Defaults to 'title' or 'name', if those fields are available. | |
$_query | array | - 'conditions': The conditional query elements, e.g. array('published' => true). - 'fields': The fields that should be retrieved. When unset or explitily set to null, '*' or left unset, defaults to all fields. - 'order': The order in which the data will be returned, e.g. array('id' => 'ASC') or array('modified' => 'DESC'). - 'limit': The maximum number of records to return. - 'page': For pagination of data (equals limit * offset). - 'with': An array of relationship names to be included in the query. | |
$_relationFieldNames | array | Matching between relation's fieldnames and their corresponding relation name. | |
$_relationTypes | array | Valid relation types are: - belongsTo - hasOne - hasMany | |
$_relations | array | A list of the current relation types for this Model. | |
$_relationsToLoad | Store available relation names for this model which still unloaded. | ||
$_schema | array | The schema is lazy-loaded by the first call to Model::schema(), unless it has been manually defined in the Model subclass. For schemaless persistent storage (e.g. MongoDB), this is never populated automatically - if you desire a fixed schema to interact with in those cases, you will be required to define it yourself. Example: protected $_schema = array( '_id' => array('type' => 'id'), // required for Mongo 'name' => array('type' => 'string', 'default' => 'Moe', 'null' => false), 'sign' => array('type' => 'string', 'default' => 'bar', 'null' => false), 'age' => array('type' => 'integer', 'default' => 0, 'null' => false) ); For MongoDB specifically, you can also implement a callback in your database connection configuration that fetches and returns the schema data, as in the following: config/bootstrap/connections.php: Connections::add('default', array( 'type' => 'MongoDb', 'host' => 'localhost', 'database' => 'app_name', 'schema' => function($db, $collection, $meta) { $result = $db->connection->schemas->findOne(compact('collection')); return $result ? $result['data'] : array(); } )); This example defines an optional MongoDB convention in which the schema for each individual collection is stored in a "schemas" collection, where each document contains the name of a collection, along with a 'data' key, which contains the schema for that collection, in the format specified above. When defining '$_schema' where the data source is MongoDB, the types map to database types as follows: id => MongoId date => MongoDate regex => MongoRegex integer => integer float => float boolean => boolean code => MongoCode binary => MongoBinData |
Méthode | Description | |
---|---|---|
__call ( string $method, array $params ) : mixed | Magic method that allows calling Model::_instanceMethods's closure like normal methods on the model instance. | |
__callStatic ( string $method, array $params ) : mixed | Enables magic finders. These provide some syntactic-sugar which allows to i.e. use Model::all() instead of Model::find('all'). | |
applyFilter ( string $method, mixed $closure = null ) | Wraps StaticObject::applyFilter() to account for object instances. | |
bind ( string $type, string $name, array $config = [] ) : object | Creates a relationship binding between this model and another. | |
config ( array $config = [] ) | Configures the model for use. This method will set the Model::$_schema, Model::$_meta, Model::$_finders class attributes, as well as obtain a handle to the configured persistent storage connection. | |
connection ( ) : object | Gets the connection object to which this model is bound. Throws exceptions if a connection isn't set, or if the connection named isn't configured. | |
create ( array $data = [], array $options = [] ) : object | Instantiates a new record or document object, initialized with any data passed in. For example: | |
delete ( object $entity, array $options = [] ) : boolean | Deletes the data associated with the current Model. | |
find ( string | object | integer $type, array $options = [] ) : mixed | The find method allows you to retrieve data from the connected data source. | |
finder ( string $name, string | callable | null $finder = null ) : callable | void | Sets or gets a custom finder by name. The finder definition can be an array of default query options, or an anonymous functions that accepts an array of query options, and a function to continue. To get a finder specify just the name. | |
hasField ( mixed $field ) : mixed | Checks to see if a particular field exists in a model's schema. Can check a single field, or return the first field found in an array of multiple options. | |
instanceMethods ( array $methods = null ) : array | Getter and setter for custom instance methods. This is used in Entity::__call(). | |
key ( mixed $values = null ) : mixed | If no values supplied, returns the name of the Model key. If values are supplied, returns the key value. | |
meta ( string $key = null, string $value = null ) : mixed | Gets or sets Model's metadata. | |
query ( array $query = null ) : mixed | Gets or sets the default query for the model. | |
relations ( string $type = null ) : array | object | void | Returns a list of models related to Model, or a list of models related to this model, but of a certain type. | |
remove ( mixed $conditions = [], array $options = [] ) : boolean | Remove multiple documents or records based on a given set of criteria. **WARNING**: If no criteria are specified, or if the criteria ($conditions) is an empty value (i.e. an empty array or null), all the data in the backend data source (i.e. table or collection) _will_ be deleted. | |
reset ( ) | Resetting the model. | |
respondsTo ( string $method, boolean $internal = false ) : boolean | Determines if a given method can be called. | |
save ( object $entity, array $data = null, array $options = [] ) : boolean | An instance method (called on record and document objects) to create or update the record or document in the database that corresponds to $entity. | |
schema ( mixed $field = null ) : array | lithium\data\Schema | Lazy-initialize the schema for this Model object, if it is not already manually set in the object. You can declare `protected $_schema = array(. | |
title ( object $entity ) : string | The title() method is invoked whenever an Entity object is cast or coerced to a string. This method can also be called on the entity directly, i.e. $post->title(). | |
update ( mixed $data, mixed $conditions = [], array $options = [] ) : boolean | Update multiple records or documents with the given data, restricted by the given set of criteria (optional). | |
validates ( object $entity, array $options = [] ) : boolean | An important part of describing the business logic of a model class is defining the validation rules. In Lithium models, rules are defined through the $validates class property, and are used by this method before saving to verify the correctness of the data being sent to the backend data source. |
Méthode | Description | |
---|---|---|
_filter ( string $method, array $params, mixed $callback, array $filters = [] ) : object | Wraps StaticObject::_filter() to account for object instances. | |
_finders ( ) : array | Returns an array with the default finders. | |
_getMetaKey ( string $key = null ) : mixed | Helper method used by meta() to generate and cache metadata values. | |
_inherit ( ) | Merge parent class attributes to the current instance. | |
_inherited ( ) | Return inherited attributes. | |
_initialize ( string $class ) : object | Init default connection options and connects default finders. | |
_instance ( string | object $name, array $options = [] ) : object | Returns an instance of a class with given config. The name could be a key from the classes array, a fully-namespaced class name, or an object. Typically this method is used in _init to create the dependencies used in the current class. | |
_key ( string $key, object $values, string $entity ) : mixed | Helper for the Model::key() function | |
_object ( ) | ||
_relationFieldName ( $type, $name ) | ||
_relations ( $type = null, $name = null ) : An | This method automagically bind in the fly unloaded relations. | |
_relationsToLoad ( ) : void | Iterates through relationship types to construct relation map. |
protected _getMetaKey ( string $key = null ) : mixed | ||
$key | string | The name of the meta value to return, or `null`, to return all values. |
Résultat | mixed | Returns the value of the meta key specified by `$key`, or an array of all meta values if `$key` is `null`. |
protected _inherit ( ) |
protected static _initialize ( string $class ) : object | ||
$class | string | The fully-namespaced class name to initialize. |
Résultat | object | Returns the initialized model instance. |
protected static _relations ( $type = null, $name = null ) : An | ||
$type | A type of model relation. | |
$name | A relation name. | |
Résultat | An | array of relation instances or an instance of relation. |
protected static _relationsToLoad ( ) : void | ||
Résultat | void |
public static applyFilter ( string $method, mixed $closure = null ) | ||
$method | string | |
$closure | mixed |
public static bind ( string $type, string $name, array $config = [] ) : object | ||
$type | string | The type of relationship to create. Must be one of `'hasOne'`, `'hasMany'` or `'belongsTo'`. |
$name | string | The name of the relationship. If this is also the name of the model, the model must be in the same namespace as this model. Otherwise, the fully-namespaced path to the model class must be specified in `$config`. |
$config | array | Any other configuration that should be specified in the relationship. See the `Relationship` class for more information. |
Résultat | object | Returns an instance of the `Relationship` class that defines the connection. |
public static config ( array $config = [] ) | ||
$config | array | Possible options are: - `meta`: Meta-information for this model, such as the connection. - `finders`: Custom finders for this model. - `query`: Default query parameters. - `schema`: A `Schema` instance for this model. - `classes`: Classes used by this model. |
public static connection ( ) : object | ||
Résultat | object | Returns an instance of `lithium\data\Source` from the connection configuration to which this model is bound. |
public static create ( array $data = [], array $options = [] ) : object | ||
$data | array | Any data that this object should be populated with initially. |
$options | array | Options to be passed to item. |
Résultat | object | Returns a new, _un-saved_ record or document object. In addition to the values passed to `$data`, the object will also contain any values assigned to the `'default'` key of each field defined in `$_schema`. |
public static find ( string | object | integer $type, array $options = [] ) : mixed | ||
$type | string | object | integer | The name of the finder to use. By default the
following finders are available. Custom finders can be added via `Model::finder()`.
- `'all'`: Returns all records matching the conditions.
- `'first'`: Returns the first record matching the conditions.
- `'count'`: Returns an integer count of all records matching the conditions.
- `'list'`: Returns a one dimensional array, where the key is the (primary)
key and the value the title of the record (the record must have a `'title'`
field). A result may look like: `array(1 => 'Foo', 2 => 'Bar')`.
Instead of the name of a finder, also supports shorthand usage with an object or
integer as the first parameter. When passed such a value it is equal to
`Model::find('first', array('conditions' => array(' |
$options | array | Options for the query. By default, accepts: - `'conditions'` _array_: The conditions for the query i.e. `'array('is_published' => true)`. - `'fields'` _array|null_: The fields that should be retrieved. When set to `null` and by default, uses all fields. To optimize query performance, limit the fields to just the ones actually needed. - `'order'` _array|string_: The order in which the data will be returned, i.e. `'created ASC'` sorts by created date in ascending order. To sort by multiple fields use the array syntax `array('title' => 'ASC', 'id' => 'ASC)`. - `'limit'` _integer_: The maximum number of records to return. - `'page'` _integer_: Allows to paginate data sets. Specifies the page of the set together with the limit option specifying the number of records per page. The first page starts at `1`. |
Résultat | mixed | The result/s of the find. Actual result depends on the finder being used. Most often this is an instance of `lithium\data\Collection` or `lithium\data\Entity`. |
public static hasField ( mixed $field ) : mixed | ||
$field | mixed | A single field (string) or list of fields (array) to check the existence of. |
Résultat | mixed | If `$field` is a string, returns a boolean indicating whether or not that field exists. If `$field` is an array, returns the first field found, or `false` if none of the fields in the list are found. |
public static instanceMethods ( array $methods = null ) : array | ||
$methods | array | |
Résultat | array |
public static query ( array $query = null ) : mixed | ||
$query | array | Possible options are: - `'conditions'`: The conditional query elements, e.g. `'conditions' => array('published' => true)` - `'fields'`: The fields that should be retrieved. When set to `null`, defaults to all fields. - `'order'`: The order in which the data will be returned, e.g. `'order' => 'ASC'`. - `'limit'`: The maximum number of records to return. - `'page'`: For pagination of data. - `'with'`: An array of relationship names to be included in the query. |
Résultat | mixed | Returns the query definition if querying, or `null` if setting. |
public static remove ( mixed $conditions = [], array $options = [] ) : boolean | ||
$conditions | mixed | An array of key/value pairs representing the scope of the records or documents to be deleted. |
$options | array | Any database-specific options to use when performing the operation. See the `delete()` method of the corresponding backend database for available options. |
Résultat | boolean | Returns `true` if the remove operation succeeded, otherwise `false`. |
public static respondsTo ( string $method, boolean $internal = false ) : boolean | ||
$method | string | Name of the method. |
$internal | boolean | Provide `true` to perform check from inside the class/object. When `false` checks also for public visibility; defaults to `false`. |
Résultat | boolean | Returns `true` if the method can be called, `false` otherwise. |
public save ( object $entity, array $data = null, array $options = [] ) : boolean | ||
$entity | object | The record or document object to be saved in the database. This parameter is implicit and should not be passed under normal circumstances. In the above example, the call to `save()` on the `$post` object is transparently proxied through to the `Posts` model class, and `$post` is passed in as the `$entity` parameter. |
$data | array | Any data that should be assigned to the record before it is saved. |
$options | array | Options: - `'callbacks'` _boolean_: If `false`, all callbacks will be disabled before executing. Defaults to `true`. - `'validate'` _boolean|array_: If `false`, validation will be skipped, and the record will be immediately saved. Defaults to `true`. May also be specified as an array, in which case it will replace the default validation rules specified in the `$validates` property of the model. - `'events'` _string|array_: A string or array defining one or more validation _events_. Events are different contexts in which data events can occur, and correspond to the optional `'on'` key in validation rules. They will be passed to the validates() method if `'validate'` is not `false`. - `'whitelist'` _array_: An array of fields that are allowed to be saved to this record. When unprovided will - if available - default to fields of the current schema and the `'locked'` option is not `false`. - `'locked'` _boolean_: Whether to use schema for saving just fields from the schema or not. Defaults to `true`. |
Résultat | boolean | Returns `true` on a successful save operation, `false` on failure. |
public static schema ( mixed $field = null ) : array | lithium\data\Schema | ||
$field | mixed | Optional. You may pass a field name to get schema information for just one field. Otherwise, an array containing all fields is returned. If `false`, the schema is reset to an empty value. If an array, field definitions contained are appended to the schema. |
Résultat | array | lithium\data\Schema |
public static update ( mixed $data, mixed $conditions = [], array $options = [] ) : boolean | ||
$data | mixed | Typically an array of key/value pairs that specify the new data with which the records will be updated. For SQL databases, this can optionally be an SQL fragment representing the `SET` clause of an `UPDATE` query. |
$conditions | mixed | An array of key/value pairs representing the scope of the records to be updated. |
$options | array | Any database-specific options to use when performing the operation. See the `delete()` method of the corresponding backend database for available options. |
Résultat | boolean | Returns `true` if the update operation succeeded, otherwise `false`. |
public validates ( object $entity, array $options = [] ) : boolean | ||
$entity | object | Model entity to validate. Typically either a `Record` or `Document` object. In the following example: ``` $post = Posts::create($data); $success = $post->validates(); ``` The `$entity` parameter is equal to the `$post` object instance. |
$options | array | Available options: - `'rules'` _array_: If specified, this array will _replace_ the default validation rules defined in `$validates`. - `'events'` _mixed_: A string or array defining one or more validation _events_. Events are different contexts in which data events can occur, and correspond to the optional `'on'` key in validation rules. For example, by default, `'events'` is set to either `'create'` or `'update'`, depending on whether `$entity` already exists. Then, individual rules can specify `'on' => 'create'` or `'on' => 'update'` to only be applied at certain times. Using this parameter, you can set up custom events in your rules as well, such as `'on' => 'login'`. Note that when defining validation rules, the `'on'` key can also be an array of multiple events. - `'required`' _mixed_: Represents whether the value is required to be present in `$values`. If `'required'` is set to `true`, the validation rule will be checked. if `'required'` is set to 'false' , the validation rule will be skipped if the corresponding key is not present. If don't set `'required'` or set this to `null`, the validation rule will be skipped if the corresponding key is not present in update and will be checked in insert. Defaults is set to `null`. - `'whitelist'` _array_: If specified, only fields in this array will be validated and others will be skipped. |
Résultat | boolean | Returns `true` if all validation rules on all fields succeed, otherwise `false`. After validation, the messages for any validation failures are assigned to the entity, and accessible through the `errors()` method of the entity object. |
protected array $_autoConfig | ||
Résultat | array |
protected array $_finders | ||
Résultat | array |
protected array $_inherits | ||
Résultat | array |
protected static array $_initialized | ||
Résultat | array |
protected array $_initializers | ||
Résultat | array |
protected array $_instanceFilters | ||
Résultat | array |
protected static array $_instanceMethods | ||
Résultat | array |
protected static array $_instances | ||
Résultat | array |
protected array $_meta | ||
Résultat | array |
protected array $_query | ||
Résultat | array |
protected array $_relationFieldNames | ||
Résultat | array |
protected array $_relationTypes | ||
Résultat | array |
protected array $_relations | ||
Résultat | array |
protected $_relationsToLoad |
protected array $_schema | ||
Résultat | array |
public array $validates | ||
Résultat | array |