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

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 
Reply


Messages In This Thread
RE: CodeIgniter 4 Entity Business Logic Questions - by iRedds - 09-11-2021, 09:41 PM



Theme © iAndrew 2016 - Forum software by © MyBB