Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include virtual fields in find with CakePHP 3?

I am trying to add an isOnline field for users which does not exists in the database. Is there a way In can return isOnline as a virtual field? I have tried adding this to the User entity:

protected function _getIsOnline() {
    return true;
}

But when I do a $user->find('all', []) the field is not included. Any ideas on how to accomplish this?

like image 390
Jeremy Avatar asked Oct 30 '22 23:10

Jeremy


1 Answers

You cannot use use virtual fields within the query object. Any filtering would have to take place after retrieving the data from the query. Virtual fields are lazy loaded, so they do not exist until required.

like image 155
Jeremy Avatar answered Nov 15 '22 05:11

Jeremy