Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 4 Model Callbacks not hashing password before insert
#6

(01-02-2021, 09:24 PM)iRedds Wrote:
(01-02-2021, 11:15 AM)josh2112o Wrote: Hi and thanks for the response. So, when you say the event triggers need to be called manually, does that mean I need to call them in the createUser() method like $this->db->beforeInsert($data)?
Thanks

PHP Code:
// 1 way 

$this->db->transStart();
$this->insert($data); // call default method with events
//or $this->save($data); If $data have key equal $this->primaryKey will be called update method else insert method
$this->db->transComplete();

// 2 way 
$this->db->transStart();
$eventData $this->trigger('beforeInsert', ['data' => $data]); // will call all methods specified in $this->beforeInsert
// or call your specific method directly $eventData = $this->beforeInsert(['data' => $data]);
$this->db->insert($eventData['data']); 
$this->db->transComplete(); 

IMHO if you make one request in the database, then it makes no sense to use transactions

Hi thanks again for the help.
I implemented the first suggestion by calling $this->insert($data) in the createUser($data) method I had created in the UserModel class and it works as expected. The password was hashed automatically by the $beforeInsert method. Thanks again for the help.
I also had posted this same issue over on StackOverflow.
The post is here and if you are are active on that site and want to answer there, I can mark it as accepted over there as well.
Reply


Messages In This Thread
RE: CodeIgniter 4 Model Callbacks not hashing password before insert - by josh2112o - 01-03-2021, 06:47 AM



Theme © iAndrew 2016 - Forum software by © MyBB