Wednesday, February 16, 2022

[FIXED] $this getting unknown property yii console

Issue

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.


Solution

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



Answered By - user7242866

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.