CodeIgniter 4 Model Callbacks not hashing password before insert |
Hi,
I am totally new to CI4 but do have some experience with PHP. I having trouble getting the Model class callback functions to work when inserting a new user row. Here is the Controller: Code: <?php Code: <?php Code: if ($this->users_model->createUser($user_data)) { In the model: Code: if($this->db->affectedRows() == 1) { Code: $this->db->affectedRows() Can anyone tell me where I am going wrong in all of this? I have successfully used the Code: password_hash() Code: $this->request->getVar('password') Thanks for any help.
At first, the $allowCallbacks model property is enabled by default.
For events to work, you need to use defined methods like insert / update / save / etc. $model->insert($yourData); But you create your own createUser method and work directly with the builder. Of course, event triggers will not work in this case. They need to be called manually. (01-02-2021, 09:41 AM)iRedds Wrote: At first, the $allowCallbacks model property is enabled by default. 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 (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)? PHP Code: // 1 way IMHO if you make one request in the database, then it makes no sense to use transactions
Also keep in mind that password_hash is expecting a string parameter, may cause problems passing arrays.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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)? 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. (01-02-2021, 09:45 PM)InsiteFX Wrote: Also keep in mind that password_hash is expecting a string parameter, may cause problems passing arrays.Thank you for the information and best practices suggestion. I guess I hadn't thought it would be any trouble if passing in the associative array key to password_hash() that it might cause issues. Thanks again.
Also be careful on the user inputting null values there have been problems with it in password_hash,
best bet is to read up on it on PHP.net What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |