Encapsulate entity properties by default


#1

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!


#2

You can hide properties

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


#3

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


#4

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