In Eden, we managed to loose couple models which takes off the restrictiveness of a normal ORM 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 the concept of "loosely defined" and here's what we came up with.Figure 13. Database Model (Extends Array)
$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@example.com'; //set as array echo $model->user_name; //access as object $model->user_name = 'firstname.lastname@example.org'; //set as object $model->save('user', $database); //save to 'user' table in database //only relavent columns will be saved
So model properties can be accesed by method, object or array. The prefrence we leave up to you. With our model, you can put extra key values in the object, even if it has nothing to do with the intended database table. When you call save(), this is when you need to specify the table your saving to. This method is really powerful, in that it will first check to see what columns exist in the table then compares it with your model. It will only save columns that have the matching column name in your object. Lastly it will auto determine whether if we should insert or update that row.
A common example is when you have an array table that comprises of joined data. In Eden you can leave that array as is then call save() for each table as in Figure 14.Figure 14. Two tables
$row = array( 'user_id' => 1, 'user_name' => 'Chris', 'user_email' => 'email@example.com', 'post_user' => 1, 'post_title' => 'My Post', 'post_detail' => 'This is my new article'); $db->model($row)->save('user')->save('post');
A lot of power and explained simply, we'll go over why models are such a key piece to the overall picture later in this section.« Back to C. SQLite 6. Collections »