onload hook

35 views
Skip to first unread message

Suki

unread,
Mar 3, 2017, 3:39:23 PM3/3/17
to Fat-Free Framework
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.

ikkez

unread,
Mar 4, 2017, 4:50:05 AM3/4/17
to Fat-Free Framework
You cannot "create" new class properties like this. If you set something to an unexisting field, it becomes a virtual/adhoc field that usually contains a SQL expression.
Try to change your code to this:

class User extends \DB\SQL\Mapper
{
 
function __construct(\DB\SQL $db)
 
{
 parent
::__construct($db, 'user');

 $this
->isOnline = "DATE(last_active - INTERVAL 5 MINUTE)";
 
}}

If you need to add class properties, create them as public variables in the class before.

Jessica González

unread,
Mar 4, 2017, 3:21:06 PM3/4/17
to f3-fra...@googlegroups.com
Thanks!  I thought it as possible because the docs uses a similar example but now I see that it refers to local properties instead of mapped fields, it can be confusing :P
Reply all
Reply to author
Forward
0 new messages