From 85a32bea4246a49df95e5d0b782922ba13b5623a Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Wed, 4 Dec 2013 21:48:43 +0200 Subject: [PATCH] Method "getChunkCollection()" added to Mongo file collection. Method file unit tests improved. --- extensions/mongo/Database.php | 4 +-- extensions/mongo/file/ActiveRecord.php | 30 +++++++++++++++++++++- extensions/mongo/file/Collection.php | 22 ++++++++++++++++ .../unit/extensions/mongo/file/CollectionTest.php | 8 ++++++ tests/unit/extensions/mongo/file/QueryTest.php | 18 ++++++++++++- 5 files changed, 78 insertions(+), 4 deletions(-) diff --git a/extensions/mongo/Database.php b/extensions/mongo/Database.php index 01d43de..fcc733f 100644 --- a/extensions/mongo/Database.php +++ b/extensions/mongo/Database.php @@ -34,7 +34,7 @@ class Database extends Object /** * Returns the Mongo collection with the given name. * @param string $name collection name - * @param boolean $refresh whether to reload the table schema even if it is found in the cache. + * @param boolean $refresh whether to reload the collection instance even if it is found in the cache. * @return Collection mongo collection instance. */ public function getCollection($name, $refresh = false) @@ -48,7 +48,7 @@ class Database extends Object /** * Returns Mongo GridFS collection with given prefix. * @param string $prefix collection prefix. - * @param boolean $refresh whether to reload the table schema even if it is found in the cache. + * @param boolean $refresh whether to reload the collection instance even if it is found in the cache. * @return file\Collection mongo GridFS collection. */ public function getFileCollection($prefix = 'fs', $refresh = false) diff --git a/extensions/mongo/file/ActiveRecord.php b/extensions/mongo/file/ActiveRecord.php index b7f6f51..35f7ad1 100644 --- a/extensions/mongo/file/ActiveRecord.php +++ b/extensions/mongo/file/ActiveRecord.php @@ -16,6 +16,11 @@ namespace yii\mongo\file; class ActiveRecord extends \yii\mongo\ActiveRecord { /** + * @var \MongoGridFSFile|string + */ + public $file; + + /** * Creates an [[ActiveQuery]] instance. * This method is called by [[find()]] to start a "find" command. * You may override this method to return a customized query (e.g. `CustomerQuery` specified @@ -49,6 +54,29 @@ class ActiveRecord extends \yii\mongo\ActiveRecord } /** + * Creates an active record object using a row of data. + * This method is called by [[ActiveQuery]] to populate the query results + * into Active Records. It is not meant to be used to create new records. + * @param \MongoGridFSFile $row attribute values (name => value) + * @return ActiveRecord the newly created active record. + */ + public static function create($row) + { + $record = static::instantiate($row); + $columns = array_flip($record->attributes()); + foreach ($row->file as $name => $value) { + if (isset($columns[$name])) { + $record->setAttribute($name, $value); + } else { + $record->$name = $value; + } + } + $record->setOldAttributes($record->getAttributes()); + $record->afterFind(); + return $record; + } + + /** * Returns the list of all attribute names of the model. * This method could be overridden by child classes to define available attributes. * Note: primary key attribute "_id" should be always present in returned array. @@ -56,7 +84,7 @@ class ActiveRecord extends \yii\mongo\ActiveRecord */ public function attributes() { - return ['id', 'file']; + return ['id', 'filename']; } /** diff --git a/extensions/mongo/file/Collection.php b/extensions/mongo/file/Collection.php index f3e8d33..d9de3f9 100644 --- a/extensions/mongo/file/Collection.php +++ b/extensions/mongo/file/Collection.php @@ -8,10 +8,12 @@ namespace yii\mongo\file; use yii\mongo\Exception; +use Yii; /** * Collection represents the Mongo GridFS collection information. * + * @property \yii\mongo\Collection $chunkCollection file chunks Mongo collection. This property is read-only. * @method \MongoGridFSCursor find() returns a cursor for the search results. * * @author Paul Klimov @@ -23,6 +25,26 @@ class Collection extends \yii\mongo\Collection * @var \MongoGridFS Mongo GridFS collection instance. */ public $mongoCollection; + /** + * @var \yii\mongo\Collection file chunks Mongo collection. + */ + private $_chunkCollection; + + /** + * Returns the Mongo collection for the file chunks. + * @param boolean $refresh whether to reload the collection instance even if it is found in the cache. + * @return \yii\mongo\Collection mongo collection instance. + */ + public function getChunkCollection($refresh = false) + { + if ($refresh || !is_object($this->_chunkCollection)) { + $this->_chunkCollection = Yii::createObject([ + 'class' => 'yii\mongo\Collection', + 'mongoCollection' => $this->mongoCollection->chunks + ]); + } + return $this->_chunkCollection; + } /** * Removes data from the collection. diff --git a/tests/unit/extensions/mongo/file/CollectionTest.php b/tests/unit/extensions/mongo/file/CollectionTest.php index a0aaa3e..acec5ed 100644 --- a/tests/unit/extensions/mongo/file/CollectionTest.php +++ b/tests/unit/extensions/mongo/file/CollectionTest.php @@ -17,6 +17,14 @@ class CollectionTest extends MongoTestCase // Tests : + public function testGetChunkCollection() + { + $collection = $this->getConnection()->getFileCollection(); + $chunkCollection = $collection->getChunkCollection(); + $this->assertTrue($chunkCollection instanceof \yii\mongo\Collection); + $this->assertTrue($chunkCollection->mongoCollection instanceof \MongoCollection); + } + public function testFind() { $collection = $this->getConnection()->getFileCollection(); diff --git a/tests/unit/extensions/mongo/file/QueryTest.php b/tests/unit/extensions/mongo/file/QueryTest.php index cbba53c..b4c2f0f 100644 --- a/tests/unit/extensions/mongo/file/QueryTest.php +++ b/tests/unit/extensions/mongo/file/QueryTest.php @@ -29,7 +29,10 @@ class QueryTest extends MongoTestCase { $collection = $this->getConnection()->getFileCollection(); for ($i = 1; $i <= 10; $i++) { - $collection->storeBytes('content' . $i, ['filename' => 'name' . $i]); + $collection->storeBytes('content' . $i, [ + 'filename' => 'name' . $i, + 'file_index' => $i, + ]); } } @@ -50,4 +53,17 @@ class QueryTest extends MongoTestCase $row = $query->from('fs')->one($connection); $this->assertTrue($row instanceof \MongoGridFSFile); } + + public function testDirectMatch() + { + $connection = $this->getConnection(); + $query = new Query; + $rows = $query->from('fs') + ->where(['file_index' => 5]) + ->all($connection); + $this->assertEquals(1, count($rows)); + /** @var $file \MongoGridFSFile */ + $file = $rows[0]; + $this->assertEquals('name5', $file->file['filename']); + } } \ No newline at end of file