• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CodeIgniter Data Mapper / Domain Objects

#1
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?
Kristian Matthews
Apple Certified Associate Mac Integration & Management 10.10
epickris.com
Reply

#2
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.
Practical CodeIgniter 3  • CodeIgniter 4 Foundations - Coming Soon • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
2 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.