mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-14 21:30:17 +08:00
Mongo File Active Record refactored.
This commit is contained in:
@@ -88,26 +88,18 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
$collection = static::getCollection();
|
$collection = static::getCollection();
|
||||||
if (array_key_exists('newFileContent', $values)) {
|
if (isset($values['newFileContent'])) {
|
||||||
$fileContent = $values['newFileContent'];
|
$newFileContent = $values['newFileContent'];
|
||||||
unset($values['newFileContent']);
|
|
||||||
unset($values['file']);
|
|
||||||
$newId = $collection->insertFileContent($fileContent, $values);
|
|
||||||
} elseif (array_key_exists('file', $values)) {
|
|
||||||
$file = $values['file'];
|
|
||||||
if ($file instanceof UploadedFile) {
|
|
||||||
$fileName = $file->tempName;
|
|
||||||
} elseif (is_string($file)) {
|
|
||||||
if (file_exists($file)) {
|
|
||||||
$fileName = $file;
|
|
||||||
} else {
|
|
||||||
throw new InvalidParamException("File '{$file}' does not exist.");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new InvalidParamException('Unsupported type of "file" attribute.');
|
|
||||||
}
|
|
||||||
unset($values['newFileContent']);
|
unset($values['newFileContent']);
|
||||||
|
}
|
||||||
|
if (isset($values['file'])) {
|
||||||
|
$newFile = $values['file'];
|
||||||
unset($values['file']);
|
unset($values['file']);
|
||||||
|
}
|
||||||
|
if (isset($newFileContent)) {
|
||||||
|
$newId = $collection->insertFileContent($newFileContent, $values);
|
||||||
|
} elseif (isset($newFile)) {
|
||||||
|
$fileName = $this->extractFileName($newFile);
|
||||||
$newId = $collection->insertFile($fileName, $values);
|
$newId = $collection->insertFile($fileName, $values);
|
||||||
} else {
|
} else {
|
||||||
$newId = $collection->insert($values);
|
$newId = $collection->insert($values);
|
||||||
@@ -136,35 +128,24 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
|
|||||||
}
|
}
|
||||||
|
|
||||||
$collection = static::getCollection();
|
$collection = static::getCollection();
|
||||||
if (array_key_exists('newFileContent', $values)) {
|
if (isset($values['newFileContent'])) {
|
||||||
$fileContent = $values['newFileContent'];
|
$newFileContent = $values['newFileContent'];
|
||||||
unset($values['newFileContent']);
|
unset($values['newFileContent']);
|
||||||
|
}
|
||||||
|
if (isset($values['file'])) {
|
||||||
|
$newFile = $values['file'];
|
||||||
unset($values['file']);
|
unset($values['file']);
|
||||||
$values['_id'] = $this->getAttribute('_id');
|
}
|
||||||
$this->deleteInternal();
|
if (isset($newFileContent) || isset($newFile)) {
|
||||||
$collection->insertFileContent($fileContent, $values);
|
$rows = $this->deleteInternal();
|
||||||
$rows = 1;
|
$insertValues = $values;
|
||||||
$this->setAttribute('newFileContent', null);
|
$insertValues['_id'] = $this->getAttribute('_id');
|
||||||
$this->setAttribute('file', null);
|
if (isset($newFileContent)) {
|
||||||
} elseif (array_key_exists('file', $values)) {
|
$collection->insertFileContent($newFileContent, $insertValues);
|
||||||
$file = $values['file'];
|
|
||||||
if ($file instanceof UploadedFile) {
|
|
||||||
$fileName = $file->tempName;
|
|
||||||
} elseif (is_string($file)) {
|
|
||||||
if (file_exists($file)) {
|
|
||||||
$fileName = $file;
|
|
||||||
} else {
|
|
||||||
throw new InvalidParamException("File '{$file}' does not exist.");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidParamException('Unsupported type of "file" attribute.');
|
$fileName = $this->extractFileName($newFile);
|
||||||
|
$collection->insertFile($fileName, $insertValues);
|
||||||
}
|
}
|
||||||
unset($values['newFileContent']);
|
|
||||||
unset($values['file']);
|
|
||||||
$values['_id'] = $this->getAttribute('_id');
|
|
||||||
$this->deleteInternal();
|
|
||||||
$collection->insertFile($fileName, $values);
|
|
||||||
$rows = 1;
|
|
||||||
$this->setAttribute('newFileContent', null);
|
$this->setAttribute('newFileContent', null);
|
||||||
$this->setAttribute('file', null);
|
$this->setAttribute('file', null);
|
||||||
} else {
|
} else {
|
||||||
@@ -191,6 +172,27 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
|
|||||||
return $rows;
|
return $rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extracts filename from given raw file value.
|
||||||
|
* @param mixed $file raw file value.
|
||||||
|
* @return string file name.
|
||||||
|
* @throws \yii\base\InvalidParamException on invalid file value.
|
||||||
|
*/
|
||||||
|
protected function extractFileName($file)
|
||||||
|
{
|
||||||
|
if ($file instanceof UploadedFile) {
|
||||||
|
return $file->tempName;
|
||||||
|
} elseif (is_string($file)) {
|
||||||
|
if (file_exists($file)) {
|
||||||
|
return $file;
|
||||||
|
} else {
|
||||||
|
throw new InvalidParamException("File '{$file}' does not exist.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new InvalidParamException('Unsupported type of "file" attribute.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refreshes the [[file]] attribute from file collection, using current primary key.
|
* Refreshes the [[file]] attribute from file collection, using current primary key.
|
||||||
* @return \MongoGridFSFile|null refreshed file value.
|
* @return \MongoGridFSFile|null refreshed file value.
|
||||||
|
|||||||
Reference in New Issue
Block a user