Skip to content

Model Helper

Sakya edited this page Dec 5, 2021 · 2 revisions

Itseasy\Model\AbstractModel

This model implement Lamians\Stdlib\ArraySerializableInterface

Any attribute in this model that has populate or getArrayCopy function will be call automatically by the parent

Filter function :

  • set<Attribute> use for filtering value before assigning value to attribute
  • get<Attribute> use for filtering value before returning attribute value

When populate / getArrayCopy function is call from AbstractModel

  1. If filter function exist for given attribute call the filter function
  2. Else if the attribute implements ArraySerializableInterface call attribute populate / getArrayCopy ( nestable )
  3. Else if the attribute has populate / getArrayCopy function call the function ( nestable )
  4. Else assign / return the value directly to / from attribute

DateTime attribute helper

New in dev-main

class Model extends RecordModel
{
   protected $start_date;
  
   public function setStartDate($date) : void
   {
      $this->start_date = $this->dateToObject($date, $this->tech_timezone, "Y-m-d H:i:s");
   }

   // This function will be call by getArrayCopy() using default arguments
   public function getStartDate(bool $as_object = false, string $format = "Y-m-d H:i:s", string $timezone="UTC")
   {
      return $this->formatDate($this->start_date, $as_object, $format, $timezone);
   }
}

Json attribute helper

New in dev-main

class NestedModel extends AbstractModel
{
   protected $id;
   protected $name;
}

class Model extends AbstractModel
{
   protected $attribute;
  
   // If attribute is a collection of AbstractModel
   public function setAttribute($attr) : void
   {
       $this->attribute = $this->jsonToObject($attr, new CollectionModel(new NestedModel()));
   }

   // This function will be call by getArrayCopy() using default arguments
   public function getAttribute($as_json = false)
   {
       return $this->objectToJson($this->attribute, $as_json);
   }

   // Required to override getArrayCopy since getAttribute can only return two type a json string or an object
   public function getArrayCopy() : array
   {
       $array = parent::getArrayCopy();
       $array["attribute"] = $this->attribute->getArrayCopy();
       return $array;
   }
}

If attribute is an AbstractModel

public function setAttribute($attr) : void
{
    $this->attribute = $this->jsonToObject($attr, new NestedModel());
}

If attribute just an array

public function setAttribute($attr) : void
{
    $this->attribute = $this->jsonToObject($attr);
}

// Omit overriding getArrayCopy since getAttribute will return either a json string or an array

Itseasy\Model\RecordModel

This model extend AbstractModel

Predefine attribute:

  • tech_creation_date : database row creation time
  • tech_modification_date : database row modification time
  • tech_timezone : timezone use, default UTC. Remove from getArrayCopy function

Database Requirement:
A database table with tech_creation_date and tech_modification_date column is required.
The column usually is set by BEFORE INSERT trigger and BEFORE UPDATE trigger

Itseasy\Model\CollectionModel

  • This model implement Lamians\Stdlib\ArraySerializableInterface
  • This model does not extend AbstractModel
  • Collection is an ArrayObject
  • Collection has populate function so it will be populate automatically when nested inside AbstractModel
  • Collection use ArrayObject builtin exchangeArray and getArrayCopy function

Clone this wiki locally