Skip to content

3.4.0

Choose a tag to compare

@dereuromark dereuromark released this 05 Apr 09:41
· 80 commits to master since this release
5bf4bbb

Improvements

Entity::require()

Implemented require() as trait method to add to your entities.
It adds a sometimes needed feature of verbosely and clearly asserting the (nested) entity data structure needed for following code.

use Shim\Model\Entity\RequireTrait;

class MyEntity extends Entity {

    use RequireTrait;

This can be useful when you have service classes or parts of the system that "know" what data inside they need to calculate or render results.
If you now add this to another part of the application, it can easily be forgotten to contain a certain relation the code inside depends on it.
With this you would get alerted right away as developer and can fix up those relations.

// in some service method at the beginning
public function buildPdf(Product $product): string {
    $product->require('supplier.company.state.country')

    //Render template
}

This avoids the usual kind of hidden and not speaking

warning: 2 :: Attempt to read property "sku" on null

deprecated: 8192 :: strlen(): Passing null to parameter # 1 ($string) of type string is deprecated

warning: 2 :: foreach() argument must be of type array|object, null given

etc then inside the business logic or rendering.
Especially the latter means that you forgot a "has many" relation here to be added. Even if there would be no results, a fetched relation and an empty array then inside the entity would not trigger such warnings.

Entity::isModifiedValue() soft deprecated

Entity::isModified() has been renamed to isModifiedValue() since it would clash with CakePHP 5.2+.
Since 5.2+ the core functionality is now properly checking isDirty(), so this method became less relevant.