Welcome Guest, Not a member yet? Register   Sign In
Builder vs. Model Queries with Entities
#1

I am trying to decide whether or not to use the built-in model queries (save, update, insert, find, etc.) or the builder class. What are the apparent advantages or disadvantages of using each if I am trying to implement entities? I am currently using builder in my model functions and I am having trouble understanding if anything is working right. From the CI4 documentation it is unclear to me. Here is some of my code.

PHP Code:
class User extends Entity
{
    protected $attributes = [
        'id'                => NULL,
        'username'          => NULL,
        'password'          => NULL,
        'role'              => NULL,
        'profile_image'    => NULL,
        'profile_views'    => NULL,
        'last_login'        => NULL,
        'about_me'          => NULL,
        'age'              => NULL,
        'gender'            => NULL,
        'occupation'        => NULL,
        'hometown'          => NULL,
        'country'          => NULL,
        'fav_shape'        => NULL,
        'fav_color'        => NULL,
        'created'          => NULL,
        'modified'          => NULL,
    ];

    protected $dates = [
        'created',
        'updated',
    ];

    public function __construct (array $data null)
    {
        parent::__construct($data);
    }

    /** 
    *  Set* Methods
    */

    public function __set(string $key$value null)
    {
        // if a set* method exists for this key use that method to insert this value
        $method 'set_' $key;

        if (method_exists($this$method)) {

            $this->$method($value);
        
        
} elseif (isset($this->attributes[$key])) {

            $this->attributes[$key] = $value;
        }
    }

    protected function set_password(string $value)
    {
        $this->attributes['password'] = password_hash($valuePASSWORD_BCRYPT);
    }

    protected function set_last_login(string $value)
    {
        $this->attributes['last_login'] = new DateTime($value);
    }

    /**
    *  Get* Methods
    */

    public function __get(string $key)
    {
        // if a get* method exists for this key use that method to insert this value
        if (method_exists($this$key)) {

            return $this->$key();
        }

        if (isset($this->$key)) {

            return $this->$attributes[$key];
        }
    }

    public function last_login($format 'Y-m-d H:i:s'
    {
        return $this->attributes['last_login']->format($format);
    }



PHP Code:
class UserModel extends Model
{
    protected $DBGroup          'default';
    protected $table            'users';
    protected $primaryKey      'id';
    protected $useAutoIncrement true;
    protected $insertID        0;
    protected $returnType      = \App\Entities\Member::class;
    protected $useSoftDelete    false;
    protected $allowedFields    = [
        'username',
        'password',
        'role',
        'profile_image',
        'profile_views',
        'last_login',
        'about_me',
        'age',
        'gender',
        'occupation',
        'hometown',
        'country',
        'fav_shape',
        'fav_color',
        'modified',
    ];

    // Validation
    protected $validationRules 'registration';
    protected $skipValidation FALSE;

    public function __construct() 
    {
        parent::__construct(); 
    }

    public function updateModified(array $data)
    {
      $modified Time::now('America/New_York''en_US');

      if ($data['modified'] === NULL) {

            $data['modified'] = $modified;
        
            
return $data;
        }
    }

    public function hashPassword(array $data
    {
        if (isset($data['password'])) {

            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);

            return $data;
        }
    }

    public function lastLogin($id)
    {
        $lastLogin Time::now('America/New_York''en_US');

        return $this->builder()
            ->set('last_login'$lastLogin)
            ->where('id'$id)
            ->update();
    }

    public function addUser($newUser)
    {
        $newUser $this->hashPassword($newUser);
        $newUser $this->updateModified($newUser);

        return $this->builder()
            ->set($newUser)
            ->insert();
    }

    public function getUser($username)
    {
      return $this->builder()
            ->where(['username' => $username])
            ->get()
            ->getRow();
    }

Reply


Messages In This Thread
Builder vs. Model Queries with Entities - by AgBRAT - 06-06-2022, 11:40 PM



Theme © iAndrew 2016 - Forum software by © MyBB