I am defining a method in ATK4 with an expression field in it:
$this->addExpression('answered')->set(function($model,$select){
return $select->dsql()
->table('answer')
->field($select->expr('if(count(*)>0,"Y","") as answered'))
->where('usecollection_id',$select->getField('id'))
->where('student_id',$this->api->auth->get('id'));
})->type('boolean')->caption('Besvaret');
It works fine on my development machine, but on the production server it throws a fatal error: PHP Fatal error: Using $this when not in object context in file.php
This is the problematic line:
$this->api->auth->get('id')
Any idea what causes this difference and how to make it work?
One way is to pass $this to enclosed function like this:
That's how you should pass variables quite often to anonymous functions in PHP.
Another possibility in this particular case is to simply use
$select->api->...
,$model->api->...
or any other ATK4 object instead ofthis->api->...
. That's because all ATK4 objects have reference to API class, so$whateverObject->api->...
will always work and always point to same API class object.