• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am currently using Codeigniter 4.1.1 how do you retrieve data from the $afterInsert

#1
I am currently using Codeigniter 4.1.1 how do you retrieve the returned data from the $afterInsert callback here are my codes
my question is how to get retrive and access the returned $data from the model inside my controller (I am fairly new to codigniter any help will be appreciated)

//controller
``
    public function newBlog(){

        if($this->request->getMethod() == 'post'){
            $model= new SampleBlogModel();
            $model->save($_POST);
        }

        echo view('SampleBlogView');
    }


``
//model
``
class SampleBlogModel extends Model{
    
    protected $table = 'testdata';
    protected $allowedFields = ['blogContent' , 'blogTitle'];
    protected $primaryKey = 'blogId';

    protected $afterInsert = ['checkName'];

    protected function checkName(array $data){
        return $data;
    }

    
}

``
Reply

#2
Your missing some things.The passed array has the id, data ( key/value pairs ) and the result of the insert method.

Event Parameters

Something like this not tested.

PHP Code:
$id     $data['id'];
$data   $data['data'];
$result $data['result']; 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
Thanks for the response .My question now is why is there a need for $afterInsert since you cannot return any value.I understand the use of $beforeInsert since you can maybe hash the password or alter the data before inserting it.This also goes for $beforeUpdate.I am just confused with the $after callbacks since you cannot really return any data since you already finished doing the function.I would like to be given an instance of when the $after is used or is useful in any case. Any help would be appreciated
Reply

#4
It can also hold error information if the result set failed.

It defines the callback method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(04-14-2021, 11:46 AM)InsiteFX Wrote: It can also hold error information if the result set failed.

It defines the callback method.

Appreciate the response , thank you.
Reply

#6
It's useful if you need to update other data, trigger events, emails, logging etc. after performing the insert. It's not meant to mutate the data you just inserted, that wouldn't make sense.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.