CodeIgniter Forums
CodeIgniter Data Mapper / Domain Objects - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: CodeIgniter Data Mapper / Domain Objects (/showthread.php?tid=67522)



CodeIgniter Data Mapper / Domain Objects - EpicKris - 03-04-2017

It would be useful to have a data mapper for domain objects.

Database Example:
Code:
CREATE TABLE user
(
   user_id INT(9) unsigned PRIMARY KEY NOT NULL AUTO_INCREMENT,
   first_name VARCHAR(255) NOT NULL,
   last_name VARCHAR(255) NOT NULL,
   email VARCHAR(255) NOT NULL,
   phone VARCHAR(255),
   deleted TINYINT(1) DEFAULT '0' NOT NULL,
   created_date_time DATETIME NOT NULL,
   updated_date_time DATETIME
);

Currently with CodeIgniter 4 you can create objects such as:
PHP Code:
class User
{
   public $user_id;
   public $first_name;
   public $last_name;
   public $email;
   public $phone;
   public $deleted;

   protected $createdDateTime;
   protected $updatedDateTime;

   public function __get($name)
   {
       if (isset($this->$name)) {
           return $this->$name;
       }
   }

   public function __set($name$value)
   {
       switch ($name) {
           case 'createdDateTime':
               $this->createdDateTime DateTime::createFromFormat('U'$value);
               break;
           case 'updatedDateTime':
               $this->updatedDateTime DateTime::createFromFormat('U'$value);
               break;
       }
   }

   public function createdDateTime($format)
   {
       return $this->createdDateTime->format($format);
   }

   public function updatedDateTime($format)
   {
       return $this->updatedDateTime->format($format);
   }


However passing the object to a model won't work with `createdDateTime` and `updatedDateTime` fields.

I create objects such as:
PHP Code:
class User
{
    protected 
userId;
    protected 
firstName;
    protected 
lastName;
    protected 
email;
    protected 
phone;
    protected 
deleted;
    protected 
$createdDateTime;
    protected $updatedDateTime;

   public function __get($name)
   {
       $name $this->name($name);

        if (
method_exists($this'get' ucfirst($name))) {
            return 
$this->{'get' ucfirst($name)}();
        } else {
            return 
$this->$name;
        }
   }

   public function __set($name$value)
   {
       $name $this->name($name);

        if (
method_exists($this'set' ucfirst($name))) {
            
$this->{'set' ucfirst($name)}($value);
        } else {
            
$this->$name $value;
        }
 
   }

    public function 
getUserId()
    {
        return 
$this->userId;
    }

    public function 
setUserId(int $userId)
    {
        
$this->userId $userId;
    }

    public function 
getFirstName()
    {
        return 
$this->firstName;
    }

    public function 
setFirstName(string $firstName)
    {
        
$this->firstName $firstName;
    }

    public function 
getLastName()
    {
        return 
$this->lastName;
    }

    public function 
setLastName(string $lastName)
    {
        
$this->lastName $lastName;
    }

    public function 
getEmail()
    {
        return 
$this->email;
    }

    public function 
setEmail(string $email)
    {
        
$this->email $email;
    }

    public function 
getPhone()
    {
        return 
$this->phone;
    }

    public function 
setPhone(string $phone)
    {
        
$this->phone $phone;
    }

    public function 
getDeleted()
    {
        return 
$this->deleted;
    }

    public function 
setDeleted(bool $deleted)
    {
        
$this->deleted $deleted;
    }

    public function 
getCreatedDateTime()
    {
        if (
$this->createdDateTime === null) {
            
$this->createdDateTime = new DateTime();
        }

        return 
$this->createdDateTime;
    }

    public function 
setCreatedDateTime($createdDateTime)
    {
        if (! 
$createdDateTime instanceof DateTime) {
            
$createdDateTime DateTime::createFromFormat('U'$createdDateTime);
        }

        
$this->createdDateTime $createdDateTime;
    }

    public function 
getUpdatedDateTime()
    {
        if (
$this->updatedDateTime === null) {
            
$this->updatedDateTime = new DateTime();
        }

        return 
$this->updatedDateTime;
    }

    public function 
setUpdatedDateTime($updatedDateTime)
    {
        if (! 
$updatedDateTime instanceof DateTime) {
            
$updatedDateTime DateTime::createFromFormat('U'$updatedDateTime);
        }

        
$this->updatedDateTime $updatedDateTime;
    }

    private function 
name(string $name): string
    
{
        return 
str_replace('_'''lcfirst(ucwords($name'_')));
    }


Again, passing the object to a model won't work with `userId`, `firstName`, `lastName`, `createdDateTime` and `updatedDateTime` fields.

And your domain objects may look different to your table fields and may even include other relational domain objects. It would be useful to map the data to and from domain objects using a mapper.

What are peoples thoughts on this?


RE: CodeIgniter Data Mapper / Domain Objects - kilishan - 03-05-2017

You're absolutely correct. The solution I put in place currently was a quick hack late one night to try and improve the compatibility of using objects for saving. I obviously didn't think it all of the way through. Smile

I'm definitely open to the idea. I know the idea of a DataMapper has been bandied about in our private group, also, so I don't believe we'd get too much argument from there. And having custom classes workable with the model is a must, I personally believe.

I believe you've opened an issue at GitHub, so we can discuss implementation details there.