Data Models¶
Data models are responsible for all interactions with the database.
Page Contents
What is a Model?¶
A model is…
Architecture¶
Data model - handles the management of the data
Data object - represents one piece of actual data, and is used to read/write.
Notes on Data Object¶
As of this writing, data objects are being converted in order to overload the ‘=’ operator. Previously you had to use $obj->set() to change the value. The object code would set a flag and the field would be updated in the database on the next save.
Now, rather than using set(), you can use the ‘=’ operator. But there are side effects to be aware of, for example:
- If the extended data class uses additional members to extend the data, then they must be declared public for the desired behavior. As an
example, a Task_obj creates a member called sub_tasks. If not declared as public, then it will to into the internal $obj->_data member. Sometimes this will not be a problem, however if you wanted to use sub_tasks in a foreach loop, then it won’t work as expected.
Deletion¶
Since there are many relationships to be aware of, let the data model architecture look after the details.
This is a callback function that you can define for any model. Basically, it allows you to get notified before a record is actually deleted from the database, so that you can perform other updates.
Errors¶
If an error occurs in a model, it should set a message in public attribute $this->last_error.
Note that there is no requirement to clear this attribute, so it cannot be used to determine if an error occurred. It should always be used in combination with a return code that indicates an error condition.