1. Go to this page and download the library: Download fgh151/yii2-upload-behavior library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fgh151 / yii2-upload-behavior example snippets
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'], 'Id']);
}
}
/**
* 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'
]
];
}
public function getPhoto()
{
return $this->hasMany(Upload::className(), ['id' => 'uploadId'])
->viaTable(UserLinkPhoto::tableName(), ['userId' => 'id']);
}