I'm wondering why newly created mapper fields set with the onload hook aren't been picked up after a call to load.
On this example I want to add a boolean isOnline mapper field based on the user's last_time active field which is already defined in the table:
class User extends \DB\SQL\Mapper
{
function __construct(\DB\SQL $db)
{
parent::__construct($db, 'user');
$this->onload(function($self){
$self->set('isOnline', ($self->last_active >= time() - 300));
});
}
If I understood correctly, the onload hook gets called after a load() call, I have a simple call to load() to get the current User data:
$currentUser = $this->_models['user']->load(array('userID' => $f3->get('SESSION.user')));
The hook should set the isOnline field but if I check it directly all I get is a null value:
var_dump($currentUser->isOnline); NULL
checking the object directly var_dump($currentUser), the field does get set:
["isOnline"]=> array(2) { ["expr"]=> string(2) "()" ["value"]=> NULL
So the hook works and it gets called. I thought it was a problem with my logic so I changed it to put a dummy string:
$self->set('isOnline', 'dummy');
However the result is the same:
["isOnline"]=> array(2) { ["expr"]=> string(7) "(dummy)" ["value"]=> NULL
Even setting the field directly after the call to load() gives me the same null result:
$currentUser = $this->_models['user']->load(array('userID' => $f3->get('SESSION.user')));
$currentUser->set('isOnline', ($currentUser->last_active >= time() - 300));
Am I missing something here? or is there another way to define custom fields? I was looking at how virtual fields work but they are required to get defined before retrieving any data so the onload() hook cannot be used for it.
I know I can just set a different var for what I want: $isOnline = $currentUser->last_active >= time() - 300);
However, I would like it to automatic instead of manually doing it for each call. And the hook does just that but for some reason I cannot get the value back.
Any help will be appreciated.