To specify source file use the [[file]] attribute. It can be specified in one of the following ways:
- string - full name of the file, which content should be stored in GridFS
- \yii\web\UploadedFile - uploaded file instance, which content should be stored in GridFS
For example:
php
$record = new ImageFile();
$record->file = '/path/to/some/file.jpg';
$record->save();
You can also specify file content via [[newFileContent]] attribute:
php
$record = new ImageFile();
$record->newFileContent = 'New file content';
$record->save();
Note: [[newFileContent]] always takes precedence over [[file]].
/** * @inheritdoc */ public function beforeSave($insert) { if (!parent::beforeSave($insert)) { return false; } $this->refreshFromEmbedded(); return true; }
public function rules() { return array_merge(parent::rules(), [[['ownerId', 'contentType'], 'required'], ['ownerId', 'exist', 'targetClass' => Number::className(), 'targetAttribute' => '_id'], [['file'], 'file']]); }
public function attributes() { return array_merge(parent::attributes(), ['mimeType']); }
public function attributes() { return array_merge(parent::attributes(), ['userId', 'contentType', 'description']); }
public function attributes() { return array_merge(parent::attributes(), ['filename', 'metadata', 'metadatanew', 'uploadDate', 'groups', 'onlineStatus', 'updated_by', 'updated_at']); }
public function attributes() { return array_merge(parent::attributes(), ['filesize', 'filetype']); }