Encapsulate entity properties by default

Is there a way to change the visibility of cake’s entity properties (variables) that are coming from the DB Schema?
After making a $query = $this->ModelTable->get() I don’t want its variables to be accessed like $query->id, it should be accessed through a method like $query->getId().

I know cake has set and get methods, but they mean nothing since you can set and change values directly.
I hope someone can help :slight_smile:

Thanks in advance!

You can hide properties

https://book.cakephp.org/3.0/en/orm/entities.html#hiding-properties

Those properties are public because the table class needs access to them.

But you can set mutators/getters. If you set up getters/setters, when you call a $entity->property it will not access a property directly but go through you getter/setter method.

https://book.cakephp.org/3.0/en/orm/entities.html#accessors-mutators

1 Like

That’s not exactly what I was looking for, but works for me! Thanks @xaphalanx :grin: