yii-starter-kit / yii2-starter-kit

Yii2 Starter Kit
http://yii2-starter-kit.terentev.net
Other
1.42k stars 646 forks source link

Implement example of multiple files upload #84

Closed trntv closed 9 years ago

trntv commented 9 years ago

https://github.com/trntv/yii2-file-kit/issues/15

vindective commented 9 years ago

@trntv where will be implemented?

trntv commented 9 years ago

In a 2-3 days.

vindective commented 9 years ago

@trntv You promised to make an example?

vindective commented 9 years ago

Please implement example :)

trntv commented 9 years ago

just returned. I'll try to find appropriate place to create an example.

vindective commented 9 years ago

Please release future

trntv commented 9 years ago

Sorry for delay. I didn't find a good place to create example within this kit and i don't want to create some component special for it. So, i'll try to explain it here.

First of all, Upload widget is "multiple" by default, so you can take any example from README. Next, widget will send an array of urls in $_POST.

$_POST = [
    'files'=>['http://example.com/image1.jpg', 'http://exmaple.com/image2.jpg', ....]
];

If you will use it with model and ActiveForm, like in standard CRUD, Yii will load it into model automaticaly by Model::load(). Then you can store it to your DB. If you are using MongoDB it will be saved as is but if you are using some relational DB, you should create a table for uploaded files and save them there in ActiveRecord::afterSave() method.

I can't explain it in more details, because i don't how you want to use it. You can ask if there is something unclear.

amstr1k commented 9 years ago

@trntv I was waiting for more detailed implementation. Can make it as a module? Say module "photo albums". Where will the album and photos attached to it

trntv commented 9 years ago

As i wrote above i don't want to create some usless syntetic component for example. But i think i can create some sort of attachements in articles.

amstr1k commented 9 years ago

@trntv Yes we have already talked about it. I think it @vindective would also be useful

vindective commented 9 years ago

Fine! I will wait...

aiddroid commented 8 years ago

here's my implements,I don't know if it is a good way to do that work.Maybe you have a better one. In ActiveRecord:

class Comment extends \yii\db\ActiveRecord
{
    public $files;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%comment}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['article_id', 'body', 'attachments'], 'required'],
            [['article_id', 'author_id', 'status', 'published_at', 'created_at', 'updated_at'], 'integer'],
            [['body'], 'string'],
            [['attachments'], 'string', 'max' => 1024],//store as json in database
            [['files'],'safe']
        ];
    }

    public function behaviors() {
        return [
            \yii\behaviors\TimestampBehavior::className(),
            [
                'class'=>  \yii\behaviors\AttributeBehavior::className(),
                'attributes'=>[
                    \yii\db\ActiveRecord::EVENT_AFTER_FIND => 'files',
                    \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => 'files',
                    \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => 'files',
                ],
                'value' => function ($event) {
                    return ($event->name == \yii\db\ActiveRecord::EVENT_AFTER_FIND) ? 
                                json_decode($event->sender->attachments,true)
                                :  json_encode($event->sender->attachments);
                },
            ]
        ];
    }

 //...more....
}

In View:

echo $form->field($model, 'files')->widget(\trntv\filekit\widget\Upload::classname(), ['url'=>['/sign-in/avatar-upload'],'maxFileSize'=>5 * 1024 * 1024,'maxNumberOfFiles'=>3]);
trntv commented 8 years ago

There is an UploadBehavior that can handle multiple files upload via relation. Why you don't use it?

aiddroid commented 8 years ago

It seems need an extra table in database.anyway, I will try it,thank for your starter-kit ,haha!!!