Welcome Guest, Not a member yet? Register   Sign In
Determine if CodeIgniter’s Model Save() is Insert or Update
#1

Hi,

I have been looking at ways of deploying CodeIgniter’s Model Save() function and would like to determine whether Save() is using Insert or Update in order to return the Insert ID, if Insert is used. If an Update is performed (in below), Call to undefined method is returned.

Is there a way of determining whether Insert or Update is performed by Save() ?

Many thanks.
PHP Code:
public function addEditData($vals) {
        if(
$this->save($vals)) {
            
log_message('notice''[SUCCESS] {file}-{line}, Data Saved - '.session('user_name'));
            
            if(
$insertID=$this->insertID()) {
                return 
$insertedID;
            }else{
                return 
true;
            }
            
        }else{
            
log_message('notice''[ERROR] {file}-{line}, Failed to Add/Edit Data - '.session('user_name'));
            return 
false;
        }
    } 
Reply
#2

(This post was last modified: 12-28-2020, 05:15 AM by 68thorby68.)

I have been playing with this functionality.

If I use
PHP Code:
$this->getInsertID() 
instead of
PHP Code:
insertID() 
CI does not throw an error.

I've checked the DB and all seems in order.

Therefore it looks like I can use the save() function to insert and update, and return the insert id when an insert is performed.

PHP Code:
public function addEditData($vals) {
        if($this->save($vals)) {
            log_message('notice''[SUCCESS] {file}-{line}, Data Saved - '.session('user_name'));
            
            
if($insertID=$this->getInsertID()) {
                return $insertedID;
            }else{
                return true;
            }
            
        
}else{
            log_message('notice''[ERROR] {file}-{line}, Failed to Add/Edit Data - '.session('user_name'));
            return false;
        }
    

I have been playing with this functionality.

If I use
PHP Code:
$this->getInsertID() 
instead of
PHP Code:
insertID() 
CI does not throw an error.

I've checked the DB and all seems in order.

Therefore it looks like I can use the save() function to insert and update, and return the insert id when an insert is performed.

PHP Code:
public function addEditData($vals) {
        if($this->save($vals)) {
            log_message('notice''[SUCCESS] {file}-{line}, Data Saved - '.session('user_name'));
            
            
if($insertID=$this->getInsertID()) {
                return $insertedID;
            }else{
                return true;
            }
            
        
}else{
            log_message('notice''[ERROR] {file}-{line}, Failed to Add/Edit Data - '.session('user_name'));
            return false;
        }
    
Reply
#3

From the docs,
Quote:This (save()) is a wrapper around the insert() and update() methods that handle inserting or updating the record automatically, based on whether it finds an array key matching the $primaryKey value:

So, to check if it is an insert() or update() method, I'll suggest you do that by checking your $primaryKey.
Check out my blog: https://lagbaja.dev
Reply
#4

Your getting an error because it is

PHP Code:
$db->insertID(); 

Also it is an integer return type so if there is no insert id I would think that it would be 0 zero.

You cannot have a 0 id in a table just my thought on this
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Also, you assign $insertID but return $insertedID

PHP Code:
if($insertID=$this->getInsertID()) {
    return $insertedID;

CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB