非法偏移类型yii2

时间:2018-11-03 15:33:03

标签: yii2

我是Yii框架的新手。我确实从Github下载了扩展名Image-cropper,但在某种程度上我的模型上出现了错误“非法偏移类型”。我对什么问题没有任何想法。但是看来我的规则方法有点错误,我也不知道。 错误位于'picture1'上。

public function rules()
{
    $session = Yii::$app->session;
    return [

        [['picture1'] => [
            'class' => 'developit\jcrop\actions\Upload',
            'url' => '/common/files/',
            'path' => Yii::getAlias('@web/img/1/memoriam/'),
            'name' => Yii::$app->user->id,
        ],],
        [['name', 'date_of_birth', 'date_of_decease','gender','background'], 'required'],
        [['date_of_birth', 'date_of_decease', 'funeral_date', 'post_dt', 'edit_dt'], 'safe'],
        [['epitaph', 'biography', 'obituary_message'], 'string','on'=>['create', 'update']],
        [['owner_id', 'background','page'], 'integer','on'=>['create', 'update']],
        [['owner_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['owner_id' => 'ID']],
        [['name', 'place_of_birth', 'funeral_address'], 'string', 'max' => 255,'on'=>['create', 'update']],
        [['gender', 'category', 'privacy_status'], 'string', 'max' => 1,'on'=>['create', 'update']],
        //[['picture1'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg, jpeg'],
        [['owner_id'], 'default', 'on'=>['create','update'], 'value'=>$session['UserID']],
        [['post_dt'], 'default', 'on'=>['create'], 'value'=> date('Y-m-d H:i:s')],


    ];
}

1 个答案:

答案 0 :(得分:0)

您在错误的位置进行操作。您应该将其附加到模型行为,而不是规则中。

public function behaviors()
    {
        return [
            [
                'class' => CropImageUploadBehavior::class,
                'attribute' => 'picture1',
                'scenarios' => ['insert', 'update'],
                'path' => '@webroot/upload/docs',
                'url' => '@web/upload/docs',
                'ratio' => 1,
                'crop_field' => 'photo_crop',
                'cropped_field' => 'photo_cropped',
                    ],
        ];
    }