Virtual field in entity, which uses data from other model

Hi, please could anyone help me with this?

Is there a possibility to load data from other model and use result as a virtual field?


Thank you!

It’s possible, but not with $this->People. This is an entity object, not a table object, so there’s no such associations you can rely on. You can use the TableRegistry to get the people table object to read from.

Ideally, though, you might not use get to always get that record, but first check whether $this->person is set (e.g. from containment done by whoever calls this). If not, set not $person but $this->person, and then if you happen to need to use this more than once in a request, the data you need is already present.

I can highly recommend installing GitHub - dereuromark/cakephp-ide-helper: IDE Helper plugin for CakePHP and let it annotate all your model files.

then your editor should have an easier time telling you what “can” be present inside your entity (if it was loaded correctly beforehand)