5. Models

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'] = 'my@email.com';	//set as array

echo $model->user_name; 				//access as object
$model->user_name = 'my@email.com';		//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'	=> 'my@email.com',
	'post_user' 	=> 1,
	'post_title'	=> 'My Post',
	'post_detail'	=> 'This is my new article');
You can also save to different databases as in save('post', $db2)

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.

© 2012 Openovate Labs. All rights reserved.