-
Notifications
You must be signed in to change notification settings - Fork 12
5.6. Model
A model is a representation of an object; simply deals with key/value pairs. Models become useful when pairing it with a data store, JSON results, services etc. Implementing this object can be done like so.
use Cradle\Data\Model;
$model1 = new Model;
//or
$model2 = new Model(['foo_bar' => 'zoo']);We managed to loosely define models which takes off the restrictiveness of other model definitions and adds scalability as an end result. First off, what we did was define a generic, yet powerful model class that can be extended, but also can be used as is. Our model class is already powerful enough to solve for a lot of use cases, you might not need to extend it. We played around with this concept and here’s what we came up with.
$model->setUserName('Chris'); //set user name
$model->getUserEmail(); // returns user email
//$model->setAnyThing() // set or get any abstract key
echo $model['user_name']; //access as array
$model['user_email'] = '[email protected]'; //set as array
echo $model->user_name; //access as object
$model->user_name = '[email protected]'; //set as object
//iterate
foreach ($model as $key => $value) {
//Do something
}When converting models to strings, a JSON string version will be the result.
echo $model1; //--> {"foo_bar":"zoo"}A model object mounts ArrayAccessTrait, CountableTrait, GeneratorTrait,
IteratorTrait, MagicTrait, DotTrait
(see Data Traits) and implements 2 additional methods.
get - Returns the entire data set.
$model2->get(); //--> ['foo_bar' => 'zoo']set - Sets the entire data set.
$model1->set(['foo_bar' => 'zoo']);2.B. Reference: Validation Types
2.D. Reference: Indexes & Relations
3.A. Reference: Cradle on Shared Hosts
3.B. Reference: Command Line Tools
3.C. Reference: Architecture Recommendations
4.4. Intro to Handlebars Templating
4.B. Reference: Handlebars Helpers
4.C. Reference: Doon Interfaces
4.D. Reference: Global Package Methods