0

I need to access a property of a class (which is a Yii2 Model), but when i refer to $this->expires_on inside a function of that class called with this code:

$int::createNextDate();

($int is a loaded yii model)

i get this error:

Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: app\commands\DailyCronController::expires_on'

So basically php looks for the property inside the controller. How can i access that model's property instead?

Edit: here's the full code

this is part of the controller used by the command line

        $assistanceTasks = \app\models\AssistanceTask::find()->all();
        foreach($assistanceTasks as $task){
            $task::createNextDate();
        }

This is the function inside the model AssistanceTask

class AssistanceTask extends \yii\db\ActiveRecord{
...
public function createNextDate(){

    if(!$this->expires_on){
        return "Error....";
    }

But apparently there's a problem with "$this"

FINAL EDIT: The problem, as pointed by Patryk, is that i used

$int::createNextDate();

instead of

$int->createNextDate();

so the function was called as if it was a static function.

dgtal
  • 193
  • 16

2 Answers2

0

You are not trying to call expires_on not on Model but on DailyCronController

0

If you have a populated model available then it should be as easy as

$model->expires_on;

or in your case

$int->expires_on;

However without seeing the whole code its really hard to say.

user2831723
  • 832
  • 12
  • 25
  • i updated the post. Anyway, the code is inside a model function, and i want to be able to recall it on a loaded model inside a controller. But while i can do it with normal controller actions, i can't do the same from the command line. – dgtal Apr 05 '17 at 16:37
  • You can't use $this with static functions. You need to instantiate the class or pass the expires_on to the static function @dgtal – user2831723 Apr 05 '17 at 18:53
  • But the function is not static (or am i wrong?), it even works from any other controller – dgtal Apr 06 '17 at 07:37
  • But you are calling it as static... Take a look at this thread http://stackoverflow.com/questions/3754786/calling-non-static-method-with – user2831723 Apr 06 '17 at 07:57