R
R
RR2015-03-24 13:07:47
Yii
RR, 2015-03-24 13:07:47

Yii2 / ActiveRecord / Why does Setting unknown property error appear?

Actually, once again I turn to the toasters)
When saving the model, the error "Setting unknown property" appears.
Model Code:

class Media extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'media';
    }
    public function rules()
    {
        return [
            [['type'], 'required'],
            [['project', 'sizes'], 'string'],
            [['parent_id', 'author_id', 'filesize', 'width', 'height', 'timeu_add'], 'integer'],
            [['type'], 'string'],
            [['filename', 'filename_original', 'path', 'thumbnail', 'thumbnail_url', 'thumbnail_path'], 'string', 'max' => 255]
        ];
    }
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'project' => 'Project',
            'type' => 'Type',
            'parent_id' => 'Parent ID',
            'author_id' => 'Author ID',
            'filename' => 'Filename',
            'filename_original' => 'Filename Original',
            'filesize' => 'Filesize',
            'width' => 'Width',
            'height' => 'Height',
            'path' => 'Path',
            'thumbnail' => 'Thumbnail',
            'thumbnail_url' => 'Thumbnail Url',
            'thumbnail_path' => 'Thumbnail Path',
            'images' => 'Images',
            'timeu_add' => 'Timeu Add',
        ];
    }
}

The part of the controller code where the error appears:
public function actionUploadFile() {
        Yii::trace('actionUploadFile start');
        if (isset($_FILES['file'])) {
            Yii::trace('actionUploadFile _FILES inner');
            $file = \yii\web\UploadedFile::getInstanceByName('file');

            $model = new Media();

            if(isset($_GET['parent_id'])) $model->setAttribute('parent_id',$_GET['parent_id']);
            if(isset($_GET['project']))   $model->setAttribute('project',  $_GET['project']);

            $model->setAttribute('type',$file->type);
            $model->setAttribute('filename_original', $file->name);

            Yii::trace('actionUploadFile model-project = '. $model->project);

            if($model->save()) {
                Yii::trace('actionUploadFile model saved');
                // модель создана, получим id
                $model->filename = MediaHelper::generateName( $model , ['isOriginal'=>1] );
                $model->$path = Yii::$app->params['media']['path']['original'] . $model->filename; /* В этой строке ошибка*/

                $model->save();

                Yii::trace( 'actionUploadFile path alias = ' . Yii::getAlias($model->$path) );

                if($file->saveAs( Yii::getAlias($model->$path) )) {
                    Yii::trace('actionUploadFile file save = ' . $model->$path );
                    // файл созранен

                    $model->setAttribute('filesize', filesize( Yii::getAlias($model->$path) ) );
                    $model->save();
                    MediaHelper::getThumbnail($model);
                    //echo \yii\helpers\Json::encode($file);
                }
            }
        }
    }

I marked the line with the error, the error itself:
exception 'yii\base\UnknownPropertyException' with message 'Setting unknown property: common\models\Media::' in C:\www\domains\blogs_project\vendor\yiisoft\yii2\base\Component.php:197
Stack trace:
#0 C:\www\domains\blogs_project\vendor\yiisoft\yii2\db\BaseActiveRecord.php(266): yii\base\Component->__set('', '@media/original...')
#1 C:\www\domains\blogs_project\backend\controllers\MediaController.php(69): yii\db\BaseActiveRecord->__set('', '@media/original...')
#2 [internal function]: backend\controllers\MediaController->actionUploadFile()
#3 C:\www\domains\blogs_project\vendor\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)
#4 C:\www\domains\blogs_project\vendor\yiisoft\yii2\base\Controller.php(151): yii\base\InlineAction->runWithParams(Array)
#5 C:\www\domains\blogs_project\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('upload-file', Array)
#6 C:\www\domains\blogs_project\vendor\yiisoft\yii2\web\Application.php(83): yii\base\Module->runAction('media/upload-fi...', Array)
#7 C:\www\domains\blogs_project\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#8 C:\www\domains\blogs_project\backend\web\index.php(18): yii\base\Application->run()
#9 {main}

It tries to store the path field, but for some reason it doesn't see it. The path field is present in the database, it is described in the rules. What's the catch? I would be very grateful for your help!

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
E
Egor Mokeev, 2015-03-24
@romkaby

Remove the dollar sign before path

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question