2017 © Pedro Peláez
 

yii2-extension yii2-attachment

Yii 2 files attachment

image

artkost/yii2-attachment

Yii 2 files attachment

  • Saturday, March 10, 2018
  • by JiLiZART
  • Repository
  • 2 Watchers
  • 3 Stars
  • 15 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Yii2 Attachments

Build Status, (*1)

This component provide ability to attach and upload, (*2)

All uploaded files by default stored in database and have TEMPORARY status., (*3)

When model with attachments save yourself, all files attached to the model change their status to permanent., (*4)

Cli Commands

php yii attachment/manager/clear clears all temporary files from system, (*5)

How to use

Configure Manager component, (*6)

return [
    'components' => [
        'attachmentManager' => [
            'class' => 'artkost\attachment\Manager',
            'storageUrl' => '@web/storage',
            'storagePath' => '@webroot/storage',
            'attachmentFileTable' => '{{%attachment_file}}'
        ]
    ]
]

Create your own type of file, (*7)

namespace app\modules\user\models;

use artkost\attachment\models\ImageFile;

class UserAvatarFile extends ImageFile
{
    const TYPE = 'userProfile';

    //subfolder of storgae folder
    public $path = 'user/profile';
}

Create model that have attachment_id field, and attach behavior to it, (*8)

ATTENTION: model can have only one instance of behavior, (*9)

/**
 * Class Profile
 * @package app\modules\user\models
 * User profile model.
 *
 * @property integer $user_id User ID
 * @property string $name Name
 * @property string $surname Surname
 * @property int $avatar_id Avatar //our attachment_id
 * @property boolean $sex
 *
 * @property User $user User
 * @property UserAvatarFile $avatar avatar file
 */
class UserProfile extends ActiveRecord
{

    public static function tableName()
    {
        return '{{%user_profile}}';
    }

    public function behaviors()
    {
        return [
            'attachBehavior' => [
                'class' => AttachBehavior::className(),
                'models' => [
                    'avatar' => [
                        'class' => UserAvatarFile::className()
                    ]
                ]
            ]
        ];
    }

    public function getAvatar()
    {
        // simply helper method with predefined conditions
        return $this->hasOneAttachment('avatar', ['id' => 'avatar_id']);
    }
}

Currently supported only FileAPI upload, but you can add yours., (*10)

Add action into controller, (*11)

namespace app\modules\user\controllers;

use artkost\attachmentFileAPI\actions\UploadAction as FileAPIUpload;
use app\modules\user\models\UserProfile;

/**
 * Profile controller for authenticated users.
 */
class ProfileController extends Controller
{

    public function actions()
    {
        return [
            'fileapi-upload' => [
                'class' => FileAPIUpload::className(),
                'modelClass' => UserProfile::className(),
                'attribute' => 'avatar'
                //'accessCheck' => function($action) {  }
            ]
        ];
    }

}

in action view file you can use widget for upload files, (*12)

use artkost\attachmentFileAPI\widgets\File as FileAPIWidget;
?>
...
<?= $form->field($model, 'preview')->widget(
    FileAPIWidget::className(),
    [
        'url' => ['upload-preview'],
        'settings' => [
            'autoUpload' => true
        ]
    ]
)->label(false) ?>

The Versions

10/03 2018

dev-develop

dev-develop

Yii 2 files attachment

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nikolay Kostyurin

file yii2 upload module

27/08 2015

dev-master

9999999-dev

Yii 2 files attachment

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nikolay Kostyurin

file yii2 upload module