2017 © Pedro Peláez
 

yii2-extension yii2-upload-behavior

Uploud system for yii2 projects

image

fgh151/yii2-upload-behavior

Uploud system for yii2 projects

  • Thursday, April 27, 2017
  • by fgh151
  • Repository
  • 1 Watchers
  • 1 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Uploud system for yii2 projects

Uploud system for yii2 projects, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist fgh151/yii2-upload-behavior "*"

or add, (*4)

"fgh151/yii2-upload-behavior": "*"

to the require section of your composer.json file., (*5)

Update database schema, (*6)

php yii migrate/up --migrationPath=@vendor/fgh151/yii2-upload-behavior/migrations

Usage

For example we have User model, which need photo. Create table and model UserLinkPhoto. Sample code:, (*7)

 'User ID',
            'uploadId' => 'Upload ID',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'doctorId']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPhoto()
    {
        return $this->hasOne(Upload::className(), ['id' => 'uploadId']);
    }
}
```

This class will store mapping entity and upload file.
Then add behavior to User model:

```php

/**
 * This field need fo form and validation
 */
public $imagesField;

public function behaviors()
{
    return [
        [
            'class' => FileUploadBehavior::className(), //Behavior class
            'attribute' => 'imagesField',
            'storageClass' => UserLinkPhoto::className(), //Mapping class
            'storageAttribute' => 'userId', //Entity indefier in mapping clas
            'folder' => 'user' //folder on server where store files, example '@frontend/web/upload/user' 
        ]
    ];
}
```

Now you can access files via hasMany property:
 
```php
public function getPhoto()
{
    return $this->hasMany(Upload::className(), ['id' => 'uploadId'])
        ->viaTable(UserLinkPhoto::tableName(), ['userId' => 'id']);
}
```

Or direct request:

```php
public function getPhotoPath()
{
    return Yii::getAlias('@frontend') . '/web/upload/user/' .
        substr(md5($this->photo->fsFileName), 0, 2) .
        '/' . $this->id . '/' . $this->photo->fsFileName;
}
```

Form field example:
```php
= $form->field($model, 'imagesField[]')->fileInput() ?>

The Versions

27/04 2017

dev-master

9999999-dev

Uploud system for yii2 projects

  Sources   Download

MIT

The Requires

 

extension yii2

27/04 2017

v0.0.1

0.0.1.0

Uploud system for yii2 projects

  Sources   Download

MIT

The Requires

 

extension yii2