Observers: Included observers
Observer_CreatedAt
This observer acts only upon before_insert and expects your model to have a created_at property which will be set to the Unix timestamp when saving for the first time.
// Just add the Observer
protected static $_observers = array('Orm\\Observer_CreatedAt');
// But adding it just for before_insert is enough
protected static $_observers = array('Orm\\Observer_CreatedAt' => array('before_insert'));
You can change the default property expected and use a mySQL timestamp instead of the defaults:
// Use mySQL timestamp instead of Unix timestamp
Orm\Observer_CreatedAt::$mysql_timestamp = true;
// Use property called just "updated" instead of "updated_at"
Orm\Observer_CreatedAt::$property = 'updated';
Observer_UpdatedAt
This observer acts only upon before_save and expects your model to have a updated_at property which will be set to the Unix timestamp when saving (also during the first time).
// Just add the Observer
protected static $_observers = array('Orm\\Observer_UpdatedAt');
// But adding it just for before_insert is enough
protected static $_observers = array('Orm\\Observer_UpdatedAt' => array('before_save'));
You can change a $mysql_timestamp and $property value on this observer just like on the CreatedAt observer.
Observer_Validation
To be written.
// Just add the Observer
protected static $_observers = array('Orm\\Observer_Validation');
// But adding it just for before_insert is enough
protected static $_observers = array('Orm\\Observer_Validation' => array('before_save'));
Observer_Typing
This is for 2 things: type enforcement for input and type casting for output from the DB. That means that when you're saving the Typing observer will try to cast the input value to the expected type and throw an exception when it can't. And when you're retrieving DB data, normally it would all be strings (even integers and floats) but with the typing observer those will be cast to their scalar type.
In addition to the above the Typing observer also adds support for serialized & json fields. Both should be string type fields ("text" preferably) but will have their value encoded for saving (using serialize() or json_encode()) and decoded when retrieving from the DB (using unserialize() or json_decode()).
The Observer_Typing isn't meant as an alternative to validation, don't try to use it as such. Neither are the exceptions thrown by this observer meant to be read by the visitor of your site, they're meant to help you debug your code.
// Just add the Observer
protected static $_observers = array('Orm\\Observer_Typing');
// But adding it just for these specific events is enough
protected static $_observers = array('Orm\\Observer_Typing' => array('before_save', 'after_save', 'after_load'));
For this observer to work you must have your the $_properties static variable set in your model, or not set at all using detection with DB::list_columns() (mySQL only!). When configuring it yourself the following settings are available:
Param | Valid input | Description |
---|---|---|
data_type | varchar, int, integer, tinyint, smallint, mediumint, bigint, float, double, decimal, text, tinytext, mediumtext, longtext, enum, set, bool, boolean, serialize, json | The SQL data type, Required to have the typing observer used on a field. |
is_nullable | bool | Whether null is allowed as a value |
character_maximum_length | int | The maximum number of characters allowed for a string data type (varchar, text) |
min | int | The minimal value for an integer |
max | int | The mixamum value for an integer |
options | array |
Array of valid string values for set or enum data type Note: currently the options themselves cannot contain comma's. |