Closed trntv closed 9 years ago
@trntv where will be implemented?
In a 2-3 days.
@trntv You promised to make an example?
Please implement example :)
just returned. I'll try to find appropriate place to create an example.
Please release future
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.
@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
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.
@trntv Yes we have already talked about it. I think it @vindective would also be useful
Fine! I will wait...
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]);
There is an UploadBehavior
that can handle multiple files upload via relation. Why you don't use it?
It seems need an extra table in database.anyway, I will try it,thank for your starter-kit ,haha!!!
https://github.com/trntv/yii2-file-kit/issues/15