yii2-file-behavior Adds file behavior to Active Records

File Behavior

  1. Features
  2. Installation
  3. Usage
  4. Futur developpment plan (when I need them)

Adds file behavior to Active Records

Features

  • Upload handeled throuth behaviors
  • Rewrite url with model variables
  • After Save update model for insert support
  • skipOnEmpty support

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist claudejanz/yii2-file-behavior "*"

or add

"claudejanz/yii2-file-behavior": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by :

In model
public function rules() {
        return array_merge(parent::rules(), [
            [['image'], 'file', 'extensions' => 'jpg'],
            [['image2'], 'file', 'extensions' => 'jpg'],
        ]);
    }

    public function behaviors() {
        return [
            'image' => [
                'class' => \claudejanz\fileBehavior\FileBehavior::className(),
                'paths' => '@webroot/images/all/{id}/',
            ],
        ];
    }

In Controller
public function actionCreate() {
        $model = new Vin;

        if ($model->loadWithFiles(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    public function actionUpdate($id) {
        /* @var $model Vin */
        $model = $this->model;
        if ($model->loadWithFiles(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

More about configuration in FileUploade comments

Futur developpment plan (when I need them)

  • Delete of old files
  • Multiple files upload for one field
1 0
4 followers
0 downloads
Yii Version: 2.0
License: GPL-3.0
Category: Validation
Developed by: klod
Created on: Aug 15, 2014
Last updated: 10 years ago

Related Extensions