lithium\data\Model::$_schema
Stores the data schema.
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
Source
protected $_schema = array();