HMVC framework for WordPress plugin development Download

H(M)VC for Models

Models

Wikipedia: The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).


Creating a Simple Model - Portfolio Item

Model class name prefixed with 'mod' e.g. You want to create a Portfolio Item Model. You name it as modPortfolio_Item. File name must be lowercase.

//File: wp-content/galleryplugin/mvc/models/portfolio/item.php
class modPortfolio_Item extends absModel
{
    protected $_primary_key = 'item_id';
    protected $_table = 'portfolio_items';
}

The above is an example of one simple model.

  • absModel: It is an abstract model class which does common tasks. Create, Update, Delete, Delete, Reading results
  • It is actually a wrapper of wpdb with specific extensions.

Portfolio Item - Model Example

//wp-content/plugins/galleryplugin/mvc/controls/portfolio/item.php
class galleryplugin_portfolio_item_controller extends absController
{    
    public function before()
    {
        $this->_model = new modPortfolio_Item();
    }	

    public function index(){...}

    public function item_page()
    {
        $item_id = (int)$this->_request->getParam('id',0);
        if($item_id == 0)
        {
            throw new exp404('Item not found');
        }
        $item_data = $this->_model->Data($item_id);
        if($item_data['item_id'] <= 0)
        {
            throw new exp404('Item not found');
        }

        $this->ViewData('item',$item_data)
             ->View('portfolio/item/page');
    }
}


WordPress Fuel - Abstract Model Class

//wp-content/plugins/wpfuel/core/classes/abstract/model.php
abstract class absModel 
{
    protected function create(array $data);

    /**
    *  Execute Create() or Update() method depending on the primary key value
       @param array $data | array('id' => 1 , 'dummy_column' => 'Sample Value')
    */
    function Save(array $data);
    
    protected function update(array $data);
    
    /**
     * @param int | array (mixed) $id
     * @param type $table
     * @param type $pk_column
     * @return type 
     */
    function delete($id);
    
    /**
     *  Delete record by Foriegn Key reference
     */
    function deleteByFKId($fk_id,$fk_value);
    
    /**
     * Check if the ID exists
     */
    function isValid($id);
    
    /**
     * @param $id
     * @param type $pk_column
     * @param type $table
     * @return Array 
     */
    function Data($id);

}

This is the very basic model abstraction as WP already provides many SQL specific features.