Commit 77f10ed9 by Paul Klimov

Mongo file Active Record saving fixed.

parent ca608a81
......@@ -92,7 +92,7 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
$fileContent = $values['newFileContent'];
unset($values['newFileContent']);
unset($values['file']);
$newId = $collection->storeBytes($fileContent, $values);
$newId = $collection->insertFileContent($fileContent, $values);
} elseif (array_key_exists('file', $values)) {
$file = $values['file'];
if ($file instanceof UploadedFile) {
......@@ -108,7 +108,7 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
}
unset($values['newFileContent']);
unset($values['file']);
$newId = $collection->storeFile($fileName, $values);
$newId = $collection->insertFile($fileName, $values);
} else {
$newId = $collection->insert($values);
}
......@@ -142,8 +142,10 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
unset($values['file']);
$values['_id'] = $this->getAttribute('_id');
$this->deleteInternal();
$collection->storeBytes($fileContent, $values);
$collection->insertFileContent($fileContent, $values);
$rows = 1;
$this->setAttribute('newFileContent', null);
$this->setAttribute('file', null);
} elseif (array_key_exists('file', $values)) {
$file = $values['file'];
if ($file instanceof UploadedFile) {
......@@ -161,8 +163,10 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
unset($values['file']);
$values['_id'] = $this->getAttribute('_id');
$this->deleteInternal();
$collection->storeFile($fileName, $values);
$collection->insertFile($fileName, $values);
$rows = 1;
$this->setAttribute('newFileContent', null);
$this->setAttribute('file', null);
} else {
$condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock();
......@@ -188,6 +192,17 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
}
/**
* Refreshes the [[file]] attribute from file collection, using current primary key.
* @return \MongoGridFSFile|null refreshed file value.
*/
public function refreshFile()
{
$mongoFile = $this->getCollection()->get($this->getPrimaryKey());
$this->setAttribute('file', $mongoFile);
return $mongoFile;
}
/**
* Returns the associated file content.
* @return null|string file content.
* @throws \yii\base\InvalidParamException on invalid file value.
......@@ -195,10 +210,18 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
public function getFileContent()
{
$file = $this->getAttribute('file');
if (empty($file) && !$this->getIsNewRecord()) {
$file = $this->refreshFile();
}
if (empty($file)) {
return null;
} elseif ($file instanceof \MongoGridFSFile) {
return $file->getBytes();
$fileSize = $file->getSize();
if (empty($fileSize)) {
return null;
} else {
return $file->getBytes();
}
} elseif ($file instanceof UploadedFile) {
return file_get_contents($file->tempName);
} elseif (is_string($file)) {
......
......@@ -63,38 +63,27 @@ class Collection extends \yii\mongo\Collection
/**
* @param string $filename name of the file to store.
* @param array $metadata other metadata fields to include in the file document.
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* unless an "_id" was explicitly specified in the metadata.
*/
public function put($filename, $metadata = [])
{
return $this->mongoCollection->put($filename, $metadata);
}
/**
* @param string $bytes string of bytes to store.
* @param array $metadata other metadata fields to include in the file document.
* @param array $options list of options in format: optionName => optionValue
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* unless an "_id" was explicitly specified in the metadata.
*/
public function storeBytes($bytes, $metadata = [], $options = [])
public function insertFile($filename, $metadata = [], $options = [])
{
$options = array_merge(['w' => 1], $options);
return $this->mongoCollection->storeBytes($bytes, $metadata, $options);
return $this->mongoCollection->storeFile($filename, $metadata, $options);
}
/**
* @param string $filename name of the file to store.
* @param string $bytes string of bytes to store.
* @param array $metadata other metadata fields to include in the file document.
* @param array $options list of options in format: optionName => optionValue
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* unless an "_id" was explicitly specified in the metadata.
*/
public function storeFile($filename, $metadata = [], $options = [])
public function insertFileContent($bytes, $metadata = [], $options = [])
{
$options = array_merge(['w' => 1], $options);
return $this->mongoCollection->storeFile($filename, $metadata, $options);
return $this->mongoCollection->storeBytes($bytes, $metadata, $options);
}
/**
......@@ -104,7 +93,7 @@ class Collection extends \yii\mongo\Collection
* @return mixed the "_id" of the saved file document. This will be a generated [[\MongoId]]
* unless an "_id" was explicitly specified in the metadata.
*/
public function storeUploads($name, $metadata = [])
public function insertUploads($name, $metadata = [])
{
return $this->mongoCollection->storeUpload($name, $metadata);
}
......
......@@ -156,8 +156,7 @@ class ActiveRecordTest extends MongoTestCase
// updateAll
$pk = ['_id' => $record->_id];
//$ret = Customer::updateAll(['status' => 55], $pk);
$ret = Customer::updateAll(['$set' => ['status' => 55]], $pk);
$ret = Customer::updateAll(['status' => 55], $pk);
$this->assertEquals(1, $ret);
$record = Customer::find($pk);
$this->assertEquals(55, $record->status);
......
......@@ -43,7 +43,7 @@ class ActiveRecordTest extends MongoTestCase
'status' => $i,
];
$content = 'content' . $i;
$record['_id'] = $collection->storeBytes($content, $record);
$record['_id'] = $collection->insertFileContent($content, $record);
$record['content'] = $content;
$rows[] = $record;
}
......@@ -116,4 +116,144 @@ class ActiveRecordTest extends MongoTestCase
$this->assertTrue($customers['1-1'] instanceof CustomerFile);
$this->assertTrue($customers['2-2'] instanceof CustomerFile);
}
public function testInsert()
{
$record = new CustomerFile;
$record->tag = 'new new';
$record->status = 7;
$this->assertTrue($record->isNewRecord);
$record->save();
$this->assertTrue($record->_id instanceof \MongoId);
$this->assertFalse($record->isNewRecord);
$fileContent = $record->getFileContent();
$this->assertEmpty($fileContent);
}
/**
* @depends testInsert
*/
public function testInsertFile()
{
$record = new CustomerFile;
$record->tag = 'new new';
$record->status = 7;
$fileName = __FILE__;
$record->setAttribute('file', $fileName);
$record->save();
$this->assertTrue($record->_id instanceof \MongoId);
$this->assertFalse($record->isNewRecord);
$fileContent = $record->getFileContent();
$this->assertEquals(file_get_contents($fileName), $fileContent);
}
/**
* @depends testInsert
*/
public function testInsertFileContent()
{
$record = new CustomerFile;
$record->tag = 'new new';
$record->status = 7;
$newFileContent = 'Test new file content';
$record->setAttribute('newFileContent', $newFileContent);
$record->save();
$this->assertTrue($record->_id instanceof \MongoId);
$this->assertFalse($record->isNewRecord);
$fileContent = $record->getFileContent();
$this->assertEquals($newFileContent, $fileContent);
}
/**
* @depends testInsert
*/
public function testUpdate()
{
$record = new CustomerFile;
$record->tag = 'new new';
$record->status = 7;
$record->save();
// save
$record = CustomerFile::find($record->_id);
$this->assertTrue($record instanceof CustomerFile);
$this->assertEquals(7, $record->status);
$this->assertFalse($record->isNewRecord);
$record->status = 9;
$record->save();
$this->assertEquals(9, $record->status);
$this->assertFalse($record->isNewRecord);
$record2 = CustomerFile::find($record->_id);
$this->assertEquals(9, $record2->status);
// updateAll
$pk = ['_id' => $record->_id];
$ret = CustomerFile::updateAll(['status' => 55], $pk);
$this->assertEquals(1, $ret);
$record = CustomerFile::find($pk);
$this->assertEquals(55, $record->status);
}
/**
* @depends testUpdate
* @depends testInsertFileContent
*/
public function testUpdateFile()
{
$record = new CustomerFile;
$record->tag = 'new new';
$record->status = 7;
$newFileContent = 'Test new file content';
$record->setAttribute('newFileContent', $newFileContent);
$record->save();
$updateFileName = __FILE__;
$record = CustomerFile::find($record->_id);
$record->setAttribute('file', $updateFileName);
$record->status = 55;
$record->save();
$this->assertEquals(file_get_contents($updateFileName), $record->getFileContent());
$record2 = CustomerFile::find($record->_id);
$this->assertEquals($record->status, $record2->status);
$this->assertEquals(file_get_contents($updateFileName), $record2->getFileContent());
}
/**
* @depends testUpdate
* @depends testInsertFileContent
*/
public function testUpdateFileContent()
{
$record = new CustomerFile;
$record->tag = 'new new';
$record->status = 7;
$newFileContent = 'Test new file content';
$record->setAttribute('newFileContent', $newFileContent);
$record->save();
$updateFileContent = 'New updated file content';
$record = CustomerFile::find($record->_id);
$record->setAttribute('newFileContent', $updateFileContent);
$record->status = 55;
$record->save();
$this->assertEquals($updateFileContent, $record->getFileContent());
$record2 = CustomerFile::find($record->_id);
$this->assertEquals($record->status, $record2->status);
$this->assertEquals($updateFileContent, $record2->getFileContent());
}
}
\ No newline at end of file
......@@ -32,12 +32,12 @@ class CollectionTest extends MongoTestCase
$this->assertTrue($cursor instanceof \MongoGridFSCursor);
}
public function testStoreFile()
public function testInsertFile()
{
$collection = $this->getConnection()->getFileCollection();
$filename = __FILE__;
$id = $collection->storeFile($filename);
$id = $collection->insertFile($filename);
$this->assertTrue($id instanceof \MongoId);
$files = $this->findAll($collection);
......@@ -49,12 +49,12 @@ class CollectionTest extends MongoTestCase
$this->assertEquals(file_get_contents($filename), $file->getBytes());
}
public function testStoreBytes()
public function testInsertFileContent()
{
$collection = $this->getConnection()->getFileCollection();
$bytes = 'Test file content';
$id = $collection->storeBytes($bytes);
$id = $collection->insertFileContent($bytes);
$this->assertTrue($id instanceof \MongoId);
$files = $this->findAll($collection);
......@@ -66,14 +66,14 @@ class CollectionTest extends MongoTestCase
}
/**
* @depends testStoreBytes
* @depends testInsertFileContent
*/
public function testGet()
{
$collection = $this->getConnection()->getFileCollection();
$bytes = 'Test file content';
$id = $collection->storeBytes($bytes);
$id = $collection->insertFileContent($bytes);
$file = $collection->get($id);
$this->assertTrue($file instanceof \MongoGridFSFile);
......@@ -88,7 +88,7 @@ class CollectionTest extends MongoTestCase
$collection = $this->getConnection()->getFileCollection();
$bytes = 'Test file content';
$id = $collection->storeBytes($bytes);
$id = $collection->insertFileContent($bytes);
$this->assertTrue($collection->delete($id));
......
......@@ -29,7 +29,7 @@ class QueryTest extends MongoTestCase
{
$collection = $this->getConnection()->getFileCollection();
for ($i = 1; $i <= 10; $i++) {
$collection->storeBytes('content' . $i, [
$collection->insertFileContent('content' . $i, [
'filename' => 'name' . $i,
'file_index' => $i,
]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment