- Seme Framework
- Credits
- Version 3.1.5
- Issue
- Deployment
Database Quick Start
The following page contains example code showing how the database class is used. For complete details please read the individual pages describing each function.
Initializing the Database Class
The following code loads and initializes the database class based on your configuration settings. Here the steps:
- Edit and change your database connection in app/config/database.php.
- Extend the SENE_Model class in your model.
- Execute the parent constructor in your model constructor.
Example:
class Name_Table_Model extends SENE_Model{
var $tbl = 'name_tabel';
var $tbl_as = 'nt';
public function __construct(){
parent::__construct();
$this->db->from($this->tbl,$this->tbl_as);
}
//execute your method below
}
Get Table Result
To obtain data from table, you can simply use manual query or query builder. Here is the example
class Name_Table_Model extends SENE_Model{
var $tbl = 'name_tabel';
var $tbl_as = 'nt';
public function __construct(){
parent::__construct();
$this->db->from($this->tbl,$this->tbl_as);
}
//execute your method below
public function get(){
//because from and select automatically executed on constructor
return $this->db->get();
}
public function getRawSQL(){
$sql = 'SELECT * FROM table_name WHERE 1';
return $this->db->query($sql);
}
}
The get method will return the array of object from table_name using query builder style. But getRawSQL get the array of object through raw SQL query.
Insert Data to Table
Adding data to table simply using query builder style or raw query style
class Name_Table_Model extends SENE_Model{
var $tbl = 'name_tabel';
var $tbl_as = 'nt';
public function __construct(){
parent::__construct();
$this->db->from($this->tbl,$this->tbl_as);
}
//execute your method below
public function set($data){
//data is array key value pair
//which is key the fieldname of table
//which is value the value of field
$res=$this->db->insert($this->tbl,$data);
if($res){
return $this->db->lastId();
}else{
return 0;
}
}
public function setRawSQL($name,$birth_date){
$sql = 'INSERT INTO table_name(id,name,birth_date)
VALUES(NULL,'.$this->db->esc($name).','.$this->db->esc($birth_date).')';
$res = $this->db->exec($sql);
if($res){
return $this->db->lastId();
}else{
return 0;
}
}
}
All method will return integer more than 0 if executed properly and will return 0 or FALSE if failed. This method also return the last inserted ID to column. So you can expand your implementation future.
Update Data on Table
To change data on table simply using query builder style or raw query style
class Name_Table_Model extends SENE_Model{
var $tbl = 'name_tabel';
var $tbl_as = 'nt';
public function __construct(){
parent::__construct();
$this->db->from($this->tbl,$this->tbl_as);
}
//execute your method below
public function update($id,$data){
//data is array key value pair
//which is key the fieldname of table
//which is value the value of field
$this->db->where('id',$id);
return $this->db->update($this->tbl,$data);
}
public function updateRawSQL($id,$name,$birth_date){
$sql = 'UPDATE table_name SET
name = '.$this->db->esc($name).',
birth_date = '.$this->db->esc($birth_date).'
WHERE id = '.$this->db->esc($id);
return $this->db->exec($sql);
}
}
All method will return integer more than 0 if executed properly and will return 0 or FALSE if failed.
Delete Data on Table
To delete data on table simply using query builder style or raw query style
class Name_Table_Model extends SENE_Model{
var $tbl = 'name_tabel';
var $tbl_as = 'nt';
public function __construct(){
parent::__construct();
$this->db->from($this->tbl,$this->tbl_as);
}
//execute your method below
public function update($id){
//data is array key value pair
//which is key the fieldname of table
//which is value the value of field
$this->db->where('id',$id);
return $this->db->delete($this->tbl);
}
public function updateRawSQL($id){
$sql = 'DELETE table_name WHERE id = '.$this->db->esc($id);
return $this->db->exec($sql);
}
}
All method will return integer more than 0 if executed properly and will return 0 or FALSE if failed.