CodeIgniter Forums
Determine if CodeIgniter’s Model Save() is Insert or Update - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Determine if CodeIgniter’s Model Save() is Insert or Update (/thread-78276.html)



Determine if CodeIgniter’s Model Save() is Insert or Update - 68thorby68 - 12-28-2020

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;
        }
    } 



RE: Determine if CodeIgniter’s Model Save() is Insert or Update - 68thorby68 - 12-28-2020

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;
        }
    



RE: Determine if CodeIgniter’s Model Save() is Insert or Update - sammyskills - 12-28-2020

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.


RE: Determine if CodeIgniter’s Model Save() is Insert or Update - InsiteFX - 12-28-2020

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


RE: Determine if CodeIgniter’s Model Save() is Insert or Update - includebeer - 12-30-2020

Also, you assign $insertID but return $insertedID

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