badbreze/yii2-attachments

此包已被废弃且不再维护。作者建议使用lispa/amos-attachments包替代。

文件上传和附加到模型扩展

安装: 519

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 47

类型:yii2-extension

1.2.12 2017-02-21 15:41 UTC

This package is not auto-updated.

Last update: 2018-02-27 09:03:02 UTC


README

Latest Stable Version Total Downloads License

文件上传和附加到模型扩展

此分支的目标是实现一些缺失的功能,例如多字段和简化安装。

演示

您可以在krajee网站上查看演示。

安装

  1. 安装此扩展的首选方式是通过composer

运行以下命令

composer require badbreze/yii2-attachments

"badbreze/yii2-attachments": ">=1.2.0"

将以下内容添加到您的composer.json文件的require部分。

  1. 将模块添加到您的主配置
<?php
'aliases' => [
    '@file' => dirname(__DIR__),
],
'modules' => [
    'file' => [
        'class' => 'file\FileModule',
        'webDir' => 'files',
        'tempPath' => '@common/uploads/temp',
        'storePath' => '@common/uploads/store',
        'tableName' => '{{%attach_file}}' // Optional, default to 'attach_file'
    ],
],

同样,将这些行添加到您的控制台配置

<?php
'controllerMap' => [
    'file' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationPath' => '@file/migrations'
    ],
],
  1. 应用迁移
php yii migrate/up --migrationPath=@vendor/badbreze/yii2-attachments/src/migrations
  1. 将行为附加到您的模型(确保您的模型有"ID"属性)
<?php
use yii\helpers\ArrayHelper;

/**
 * Declare file fields
 */
public $my_field_multiple_files;
public $my_field_single_file;

/**
 * Adding the file behavior
 */
public function behaviors()
{
    return ArrayHelper::merge(parent::behaviors(), [
        'fileBehavior' => [
            'class' => \file\behaviors\FileBehavior::className()
        ]
    ]);
}

/**
 * Add the new fields to the file behavior
 */
public function rules()
{
    return ArrayHelper::merge(parent::rules(), [
        [['my_field_multiple_files', 'my_field_single_file'], 'file'],
    ]);
}
  1. 确保您已将'enctype' => 'multipart/form-data'添加到ActiveForm选项中

  2. 确保您在模块规则中指定了maxFiles,并在AttachmentsInput中指定了maxFileCount为所需的数量

  3. 您现在可以开始使用了,查看如何使用