• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Separation of Business logic Object from Model

#1
[eluser]Unknown[/eluser]
Hi,

I am trying to work by UML Class diagram, so I whanted to have separatly the classes that
are related to business logic from model. So bear with me.


the business class:
Code:
class Category{
    public $id;
    public $description;
    
    public function __edit(){
        $this->CI =& get_instance();
        $sql = "UPDATE category SET description = '".$this->description."' WHERE id = ".$this->id;
        $this->CI->db->query($sql);
    }
    public function __add(){
        $this->CI =& get_instance();
        $sql = "INSERT category SET description = '".$this->description."'";
        $this->CI->db->query($sql);
    }
    public function __delete(){
        $this->CI =& get_instance();
        $sql = "DELETE FROM category WHERE id = ".$this->id;
        $this->CI->db->query($sql);
    }
}

now i have a class that fills the business object with data from models::method/s:

Code:
class Bop{ //business object proccessor :|
    
    private $record;  // <-- $row type array
    private $obj;     // business object
    
    public function __init($record, &$object){
        $this->obj =& $object;
        $this->record = $record;
        if(!is_object($this->obj)){
            throw new Exception('Not an object');
        }
        if(!is_array($this->record)){
            throw new Exception('Not an array');
        }
        return $this->fill();
    }
    
    private function fill(){
        foreach($this->record as $attribute=>$value){
            if($this->obj_has_atribute($attribute)){
                $this->object->$attribute = $value;
            }
        }
        return $this->object;
        
    }
    private function obj_has_atribute($attribute){
        $object_vars = get_object_vars($this->obj);
        return array_key_exists($attribute, $object_vars);
    }
}


model:
Code:
class M_Categories extends Model {
    
    /**
     * @var object
     */
    private $category;
    
    public function __construct(){
        parent::Model();
        $this->load->library('bop');
    }

    public function __init(&$category){
        $this->category =& $category;
    }
    
    public function __get_all($offset, $rows_per_page){
        $row = array();
        $sql = "SELECT * FROM category WHERE 1 ORDER BY id DESC LIMIT ".$offset.",".$rows_per_page;
        $Q = $this->db->query($sql);
        if($Q->num_rows() > 0){
            foreach ($Q->result_array() as $row){
                $category_copy = clone $this->category;
                $categoryes_array[] = $this->bop->__init($row, $category_copy);
            }
        }
        
        return $categoryes_array; // array of objects
    }
    public function __get($id){
        $sql = "SELECT * FROM category WHERE id = ".$id;
        $Q = $this->db->query($sql);
        $row = $Q->row_array();
        return $this->bop->__init($row, $this->category);
    }
}

and finaly the controller:
Code:
class Categories extends Controller{

        public function __construct(){
        parent::__construct();
        $this->load->model('m_categories');
    }
    public function index(){
    
    
        $category = new Category();
        $this->m_categories->__init($category);
        $pg_data['container'] = $this->m_categories->__get_all($start,$rp);
        $this->load->view('admin_view',$pg_data);
    
    }
    public function view(){
        
        $category = new Category();
        $this->m_categories->__init($category);
        $pg_data['container'] = $this->m_categories->__get($this->uri->segment(3));
        $this->load->view('admin_view',$pg_data);
    
    }
    public function edit(){
        
        $category = new Category();
        $category->id = $_POST['id'];
        $category->description = $_POST['category'];
        $category->__edit();
        
        redirect('categories/index/');
    }
    
    public function addnew(){
        if(strlen($_POST['category']) > 2 ){
            $category = new Category;
            $category->description = $_POST['category'];
            $category->__add();
            
            redirect('categories/index/');
        }
        redirect('categories/add/');    
    }
}

OK, does this make any sense, doing it like this?

Thanx

#2
[eluser]bigtony[/eluser]
ALL of your database stuff should be in models, so you ought to either move the functions you have in Category into M_Categories (and dispose of Category) or change the category functions to invoke functions in the model that perform the db manipulation.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.