Model

Model is a class contain about communication from framework to database. Model is a bridge for data that can be fetch or push to database from controller. The method of model can only executed from controller class.

class Blog_Model extends SENE_Model{
	var $tbl = 'blog';
	var $tbl_as = 'b';
	public function __construct(){
		parent::__construct();
		$this->db->from($this->tbl,$this->tbl_as);
	}
}
					

Composition

Model contain about class name which is match with its file name. Filename only use lower case, but there is no restriction on class name for using uppercase or camel case. Method only Extended by Sene_Model or extending from core model that extend sene_model before. Same as controller, class model have to re-executed their parent constructor using parent::__construct(); to get all functional of model.

Naming Standard

Actually this not a requirement, but Seme Framework has recommendation for naming model. Model on Seme Framework located at app/model/ you create your new file model inside that folder. But, if you have bigger apps you can start considering separated model inside folder. Seme Framework recommend the folder splitted by Major Use for creating folder, such as Front stand for frontend, admin stands for admin page, api stands for API development etc.

Different from directory name, Seme Framework suggest give name of model like exactly table name in your working database fill it with suffix model for avoiding colision with controller class name. Here is the example scheme.

app/model/
-front/
--user_model.php
--blog_model.php
-admin/
--user_model.php
--blog_model.php
					

Using Query Builder

Seme Framework model allowed you to use our query builder method that can help reuse your code in for another model. So, you can simply use copy-paste and change the $tbl properties of your class model. Here is the example:

class Blog_Model extends SENE_Model{
	var $tbl = 'blog';
	var $tbl_as = 'b';
	public function __construct(){
		parent::__construct();
	}
	public function getAll(){
		$this->db->from($this->tbl,$this->tbl_as);
		return $this->db->get();
	}
	public function countAll(){
		$this->db->select_as('COUNT(*)','total',0);
		$this->db->from($this->tbl,$this->tbl_as);
		$d = $this->db->get_first();
		if(isset($d->total)) return $d->total;
		return 0;
	}
	public function getById($id){
		$this->db->where('id',$id);
		$this->db->from($this->tbl,$this->tbl_as);
		return $this->db->get_first();
	}
	public function insert($di=array()){
		$this->db->insert($this->tbl,$di);
		return $this->db->last_id;
	}
	public function edit($id,$du=array()){
		$this->db->where('id',$id);
		return $this->db->update($this->tbl,$du);
	}
	public function delete($id){
		$this->db->where("id",$id);
		return $this->db->delete($this->tbl);
	}
}
					

Debug Query

Seme Framework has debugging feature on each Query Builder methods.

  • $this->db->get('object',1)
  • $this->db->get_first('object',1)
  • $this->db->insert($this->tbl,$di,0,1)
  • $this->db->update($this->tbl,1);
  • $this->db->delete($this->tbl,1);