Welcome Guest, Not a member yet? Register   Sign In
Entity fields not saving when using 'business logic' method.
#1

(This post was last modified: 01-21-2020, 03:47 AM by larry-ns. Edit Reason: Clarification . )

Some help / advice requested, please.

Using the 'UserModel' model and 'User' entity examples duplicated from the CodeIgniter4 docs, I have encountered a problem. The saving of the user entity to the the database works as expected until I add a 'business logic' method to the User entity class, the field involved - in this case it's the password hash example from the docs - is no longer included in the insert query when using save or insert. As follows: (The queries are copied from the CodeIgniter diagnostics panel and the I have included a var_dump of the user entity after using the fill method on the posted data from my three field form. If any other information is required, please let me know. )

PHP Code:
<?php namespace App\Entities;
  use CodeIgniter\Entity;
  class User extends Entity{    
   
// without a method
   

Code:
User
[color=#000000][size=small]array(0) { } ["dates":protected]=> array(3) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" [2]=> string(10) "deleted_at" } ["casts":protected]=> array(0) { } ["attributes":protected]=> array(5) { ["username"]=> string(11) "usertest101" ["password"]=> string(8) "12121212" ["pass_confirm"]=> string(8) "12121212" ["email"]=> string(18) "[email protected]" ["registerform"]=> string(8) "Register" } ["original":protected]=> array(0) { } ["_cast":"CodeIgniter\Entity":private]=> bool(true) }[/size][/color]

Code:
Database
SELECT 1 FROM `users` WHERE `username` = 'usertest101' LIMIT 1

INSERT INTO `users` (`username`, `password`, `email`, `created`, `updated`) VALUES ('usertest101', '12121212', '[email protected]', '2020-01-20 10:38:19', '2020-01-20 10:38:19')

Correct query with password included.

But when a method is added to hash the password, the password is no longer included in the query.
PHP Code:
<?php namespace App\Entities;

  use CodeIgniter\Entity;

  class User extends Entity{    
    
protected function setPassword(string $pass)
    {
        $this->password password_hash($passPASSWORD_BCRYPT);
        return $this;
    }        
  


User
Code:
array(0) { } ["dates":protected]=> array(3) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" [2]=> string(10) "deleted_at" } ["casts":protected]=> array(0) { } ["attributes":protected]=> array(4) { ["username"]=> string(11) "usertest102" ["pass_confirm"]=> string(8) "12121212" ["email"]=> string(18) "[email protected]" ["registerform"]=> string(8) "Register" } ["original":protected]=> array(0) { } ["_cast":"CodeIgniter\Entity":private]=> bool(true) ["password"]=> string(60) "$2y$10$y/K7qeUIilBrZeeDaCr6b.t2UZZy/CoHPFShNTNz3sSdzKPR/iop2" }

Code:
Database
SELECT 1 FROM `users` WHERE `username` = 'usertest102' LIMIT 1

INSERT INTO `users` (`username`, `email`, `created`, `updated`) VALUES ('usertest102', '[email protected]', '2020-01-20 10:57:26', '2020-01-20 10:57:26')

The password field is no longer included in the query. Does anyone have any suggestions? The hashed password can be read OK from the entity object but why is it no longer included in the INSERT statement when using the entity save or insert methods? Am I misunderstanding something fundamental here? Your advice would be much appreciated.

Thank you.
Reply


Messages In This Thread
Entity fields not saving when using 'business logic' method. - by larry-ns - 01-20-2020, 10:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB