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
#2

What does "or" mean?
Model and QueryBuilder are different entities.
QueryBuilder generates SQL code.
The model provides basic methods for working with the database using the QueryBuilder under the hood. As well as some features, like filling in timestamps or firing events before/after executing queries to the database.

Adding a constructor just to call the parent constructor is pointless by itself.
Reply
#3

I mean is it required that you use Model queries when implementing Entity Classes.
Reply
#4

(This post was last modified: 06-07-2022, 03:46 PM by kenjis.)

(06-07-2022, 12:19 PM)AgBRAT Wrote: I mean is it required that you use Model queries when implementing Entity Classes.

No.

But if you want to use CodeIgniter\Model features, you need to use CodeIgniter\Model methods.
CodeIgniter\Model is kind of Query Builder wrapper. It is very confusing CodeIgniter\Model and Query Builder
if you don't know Query Builder very well.

If you never use CodeIgniter\Model features, a model like this is simple.
https://codeigniter4.github.io/CodeIgnit...l-creation
Reply




Theme © iAndrew 2016 - Forum software by © MyBB