A. Model

First off let's create a folder called post inside of the module folder. Inside of that post folder create a file called model.php and paste the following code from Figure 3.

Figure 3. Post Model
<?php //-->
class Post_Model extends Eden_Sql_Model {
	/* Constants
	-------------------------------*/
	/* Public Properties
	-------------------------------*/
	/* Protected Properties
	-------------------------------*/
	protected $_table = 'post';
	
	/* Private Properties
	-------------------------------*/
	/* Magic
	-------------------------------*/
	public function __construct() {
		$this->_database = Eden::i()->getActiveApp()->getDatabase();
	}
	
	/* Public Methods
	-------------------------------*/
	/**
	 * Return model
	 *
	 * @param scalar|null
	 * @param string
	 * @return this
	 */
	public function load($value, $column = NULL) {
		//argument testing
		User_Error::i()
			->argument(1, 'scalar', 'array')	//argument 1 must be a scalar or array
			->argument(2, 'string', 'null');	//argument 1 must be a string or null
		
		if(is_array($value)) {
			return $this->set($value);
		}
		
		if(is_null($column)) {
			$meta = $this->_getMeta();
			$column = $meta['primary'];
		}
		
		$row = $this->_database->getRow($this->_table, $column, $value);
		
		if(is_null($row)) {
			return $this;
		}
		
		return $this->set($row);
	}
	
	/**
	 * Returns post author's information
	 *
	 * @return array
	 */
	public function getUser() {
		$userId = $this->getPostUser();
		if(!$userId) {
			return array();
		}
		
		return $this->_database->getRow('user', 'user_id', $userId);
	}
	
	/* Protected Methods
	-------------------------------*/
	/* Private Methods
	-------------------------------*/
}

In the figure above, we defined 3 methods. The constructor simply sets the database we are going to use. You can change the constructor of course if you want to allow the ability to accept a database object passed as an argument if you like. The next thing is we defined is a load method which populates the model given the post ID. The last method we defined would return more details of the user that made this post. Now that we have our model defined, open front/page/index.php and inside the render() method before the return, paste in Figure 4.

Figure 4. Test the model
front()->Post_Model()
	->setUserName('Chris')
	->setUserEmail('sample@email.com')
	->setUserPassword(md5('123'))
	->setUserCreated(time())
	->setUserUpdated(time())
	->formatTime('user_created')
	->formatTime('user_updated')
	->save('user')
	
	->setPostTitle('My First Post')
	->setPostDetail('I really hope this works.')
	->copy('user_id', 'post_user')
	->setPostCreated(time())
	->setPostUpdated(time())
	->formatTime('post_created')
	->formatTime('post_updated')
	->save()
	
	->debug('_data');

If you check your website again you'll see an array with all the data you just entered. At the same time, if you check your database, you will see that a row has been inserted in the post table as well as the user table. When we extend Eden_Sql_Model our post model becomes quite powerful as in Figure 4. Also in the figure above, although this is a post we are free to add user data side by side which reduces the amount of data structues your application needs to manage. In Eden, we managed to loose couple models which takes off the restrictiveness of a normal ORM and adds scalability as an end result.

Figure 5. Eden_Sql_Model Power
$model->setPostTitle('Title 1');			//set the post title
$model->getPostDetail();					// returns post details

echo $model['post_title'];					//access as array
$model['post_detail'] = 'This is a sample';	//set as array

echo $model->post_title; 					//access as object
$model->post_detail = 'This is a sample';	//set as object

$model->save();		//save to 'post' table in database

The figure below shows an example of utilizing our custom load() and getUser() method.

Figure 6. Loading the Model
front()->Post_Model()
	->load(1)
	->debug(true)
	->getUser();

© 2012 Openovate Labs. All rights reserved.