Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 4 Entity Business Logic Questions

I am trying to use and understand the Entity Class in CodeIgniter 4. I am wondering how to access other (same) Entity properties within the setSomeProperty() methods if I want to enforce some business logic for one of the properties prior to say insert.
For starters I don't fully understand the concept (even outside CodeIgniter in vanilla PHP) so I could absolutely be misunderstanding what to use Entities best for.

For an oversimplified example (that probably makes no sense):
Suppose in an Entity Class I have:

public function setSomeProperty(float $another_property) {
if (round(($this->attributes['another_property'] / 5280) * 4) < 4) {
$this->attributes['some_property'] = 4;
} else {
$this->attributes['some_property'] = round(($this->attributes['another_property'] / 5280) * 4);
return $this;

A couple of questions on this..
1. Is this the correct way to access other properties in this set method?
2. What if I have more than one property I need to access? Does the set method accept multiple parameters like:
setSomeProperty(float $another_property, int $another_property2)
.Thanks for help and advice.

define it
protected filed;
access it
Enlightenment  Is  Freedom

You can think of an Entity as representing a single record in a database table. Where object properties are fields in the table.
The Enitity class provides the ability to manipulate data when getting or setting field values.

PHP Code:
$entity->field 1// $this->attributes['field'] = 1; 

If you have defined a named setter, then it will be called instead $this->attributes['field'] = 1;

PHP Code:
public setField($value
  $this->attributes['field'] = $value;
  return $this;
$entity->field 1// The method will be called as $this->setField(1); 
You can use $entity->field = 1; or $this->setField(1);

But if a type cast is specified for a field, then in the first case it will work automatically, and in the second it will not.

BL example 

PHP Code:
public function setPassword(string $password)
    $this->attributes['password'] = SomePasswordHash($password);
    $this->password_expires 'some date';
// Each time the password is changed, its expiration date will change. 

Setter must get the value of the field. Otherwise, you could shoot yourself in the leg.
If you need to calculate the value of a field based on the value of another field, do not use the set[Fieldname] pattern as the method name.

For example, rename the setSomeProperty method to calcSomeProperty and call it manually.

You can pass multiple arguments to the method, but in this case you need to call this method directly. Otherwise it will throw an error.
PHP Code:
public function setSomeProperty(float $another_propertyint $another_property2) {}
$enitity->some_property 'another_property'// error 

Hi iRedds,
Thank you for the example code and great explanation. I have better understanding now of what the Entity setter and getter are used for.

Theme © iAndrew 2016 - Forum software by © MyBB