- Seme Framework
- Credits
- Version 3.1.5
- Issue
- Deployment
Insert Method
The insert
method purpose is for executing INSERT
SQL command in query.
This method also will put name of column to insert with their data.
Basic Usage
Here is the basic usage insert
method from $db
property on SENE_Model
class.
$this->db->insert(string $table_name, array $data_insert [, bool $mass_insert=0 [, bool $is_debug=0] ] ): bool
Parameters
Insert method has 2 required parameters that is $table_name and $data_insert in key value format.
$table_name
The $table_name
refers to the name of the table to which the data is to be inserted.
$data_insert
The $data_insert
value can contain key value pair in array.
The key refer to column name of the table and the value refer to value that will be inserted.
This value supported MySQL builtin functions and values, such as:
NOW()
NULL
$mass_insert
The value of $mass_insert
can contain values 1 and 0. If the value is equal to 1 (one), it is used to insert more than one row of data in one method call.
For this method to work properly, the value of $data_insert
must be of data type array of array
.
Where the first array contains a sequence or row sequence, with an automatic key.
Meanwhile, for the contents of each row the array sequence contains a combination of array keys and values as in insert data in general.
$is_debug
The $is_debug
parameter is a marker (flag) to enable debug mode.
The value of this parameter can be filled with int 1
to enable debug mode and display the query to be processed.
Fill it with another value to not enable debug mode.
In debug mode, there will be no query execution process to the database system.
Example usage
This examples will show implementation insert
method on class model and then how to executing the method on controller class.
The Model Class Example
Here is the source code example for Blog_Model
class.
<?php
class Blog_Model extends SENE_Model{
var $tbl = 'blog';
var $tbl_as = 'b';
public function __construct(){
parent::__construct();
}
public function insert($di){
$this->db->insert($ths->tbl,$di);
}
}
The Controller Class Example
Here is the source code for controller class example using insert
method.
<?php
class Blog extends SENE_Controller{
public function __construct(){
parent::__construct();
$this->load('blog_model','bm'); #class scope model
}
public function index(){
$di = array();
$di['id'] = 1;
$di['title'] = "Seme Blog!";
$di['content'] = "Hello, welcome to Seme Blog";
$di['date_publish'] = "NOW()";
$res = $this->bm->insert($di); //call the method on the model
if($res){
echo 'Success';
}else{
echo 'failed';
}
}
}
SQL Result
The following is the SQL command that generated from Blog
controller while executing insert
method.
INSERT INTO blog (id,title,content,date_publish)
VALUES
(1,"Seme Blog!","Hello, welcome to Seme Blog",NULL)
;