The Model Class

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.

<?php
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

SENE_Model Class

SENE_Model class is a base class that has various functions (methods) in it for communication purposes with database systems for web-based application development using the Seme Framework. There are many methods and properties already available in this SENE_Model class.

Class Synopsis

The following is a class synopsis for SENE_Model class.

abstract class SENE_Model
{
  public $db;
  protected $directories;
  protected $config;
  public $field = array();

  public function __construct(){ ... }
  private function loadEngine($db) { ... }
  public function __encrypt($val) { ... }
  public function __decrypt($val) { ... }
  public function __decrypt($val) { ... }
}

$db Property

The $db property of the SENE_Model class contains properties and methods for interacting with the system database. The purpose of this class property is to generate SQL code that is suitable for a particular database system without the need to memorize SQL code. In this property there is also a collection of methods to perform the Query Builder process.

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:

<?php
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 set($di=array()){
    $this->db->insert($this->tbl,$di);
    return $this->db->last_id;
  }
  public function update($id,$du=array()){
    $this->db->where('id',$id);
    return $this->db->update($this->tbl,$du);
  }
  public function del($id){
    $this->db->where("id",$id);
    return $this->db->delete($this->tbl);
  }
}

Debug Query

Seme Framework has debugging feature (flag) 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);