- Seme Framework
- Credits
- Version 3.1.5
- Issue
- Deployment
Last ID Method
The last_id
method purpose is for getting postive integer value if an ID has generated automatically from MySQL Auto Increment
feature.
Basic Usage
Here is the basic usage last_id
method from $db
property on SENE_Model class.
$this->db->lastId(): integer
Parameters
This method doenst need any parameter.
Example
On this example will show the process of 2 table, that first table will produce the last_id
, and then the second table will requires the last_id
result.
Also, with the implementation in a controller class.
The 1st Model Class
In the first example for implementation last_id
method in a model.
<?php
class D_Order_Model extends SENE_Model{
var $tbl = 'd_order';
var $tbl_as = 'dor';
public function __construct(){
parent::__construct();
}
...
public function set($di){
$this->db->insert($ths->tbl,$di);
return $this->db->lastId();
}
...
}
The 2nd Model Class
In the second example create the insert method that requires from last_id
result.
<?php
class D_Order_Detail_Model extends SENE_Model{
var $tbl = 'd_order_detail';
var $tbl_as = 'dod';
public function __construct(){
parent::__construct();
}
...
public function set($dis){
return $this->db->insert_multi($ths->tbl,$dis);
}
...
}
Controller Class
In this controller class will show how to use last_id
method, from a model and then inserted new data into another model.
<?php
class Penjualan extends SENE_Controller{
public function __construct(){
parent::__construct();
$this->load('d_order_model','dom');
$this->load('d_order_detail_model','dodm');
}
...
public function submit(){
$di = array();
$di['buyer_name'] = "Aldy Iqbal";
$di['grand_total'] = "100";
$di['date_create'] = "NOW()";
$last_id = $this->dom->set($di);
if($last_id){
$dis = array();
$di = array();
$di['d_order_id'] = $last_id;
$di['product_name'] = "Hand Sanitizer";
$di['price'] = "25";
$di['qty'] = "2";
$dis[] = $di;
$di = array();
$di['d_order_id'] = $last_id;
$di['product_name'] = "Candy";
$di['price'] = "10";
$di['qty'] = "5";
$dis[] = $di;
$res = $this->dodm->setMass($dis);
if($res){
echo 'Success';
}else{
echo 'failed, ideally we have to rollback the first insert, but in this example has no rollback method';
}
}else{
echo 'failed';
}
}
...
}