CodeIgniter Forums

Full Version: I am currently using Codeigniter 4.1.1 how do you retrieve data from the $afterInsert
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
``
[font=Consolas,]class SampleBlogModel extends Model{
    
    protected $table = 'testdata';
    protected $allowedFields = ['blogContent' , 'blogTitle'];
    protected $primaryKey = 'blogId';

    protected $afterInsert = ['checkName'];

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

    
}
[/font]

``
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']; 
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
It can also hold error information if the result set failed.

It defines the callback method.
(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.
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.