承接 fgh151/yii2-upload-behavior 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fgh151/yii2-upload-behavior

最新稳定版本:v0.0.4

Composer 安装命令:

composer require fgh151/yii2-upload-behavior

包简介

Uploud system for yii2 projects

README 文档

README

Uploud system for yii2 projects

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to the require section of your composer.json file.

Update database schema

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:

<?php

namespace common\models\user;

use common\models\user\User;
use fgh151\upload\models\Upload;
use Yii;

/**
 * This is the model class for table "userLinkPhoto".
 *
 * @property int $userId
 * @property int $uploadId
 *
 * @property User $user
 * @property Upload $photo
 */
class UserLinkPhoto extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'userLinkPhoto';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['userId', 'uploadId'], 'required'],
            [['userId', 'uploadId'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'userId' => '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:

/**
 * 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:

public function getPhoto()
{
    return $this->hasMany(Upload::className(), ['id' => 'uploadId'])
        ->viaTable(UserLinkPhoto::tableName(), ['userId' => 'id']);
}

Or direct request:

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:

<?= $form->field($model, 'imagesField[]')->fileInput() ?>

统计信息

  • 总下载量: 202
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-04-27