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-Kennington
Apple Certified Associate Mac Integration & Management 10.10