V
V
v_i_kaisarov2020-07-29 16:59:19
Yii
v_i_kaisarov, 2020-07-29 16:59:19

Yii2 REST Why public $modelClass = 'app\models\Photo'; is null?

I'm trying to load images, but for some reason the model returns null

My model

class Photo extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'photo';
    }

    public $imageFile;

    public function rules() {
        return [
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
        ];
    }
    
    public function upload()
    {
        if ($this->validate()) {
            $this->imageFile->saveAs('img/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
         } else {
             return false;
         }
     }
}

My controller
class PhotoController extends ActiveController {
    public $modelClass = 'app\models\Photo';
    public $enableCsrfValidation = false;

    public function actions() {
        $actions = parent::actions();
        unset($actions['create']);
        return $actions;
    }

    public function actionCreate() {
        $modelClass->imageFile = UploadedFile::getInstance($modelClass, 'imageFile');
        $modelClass->upload()

        return $modelClass;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question