Browse Source

Method "getChunkCollection()" added to Mongo file collection.

Method file unit tests improved.
tags/2.0.0-beta
Klimov Paul 11 years ago
parent
commit
85a32bea42
  1. 4
      extensions/mongo/Database.php
  2. 30
      extensions/mongo/file/ActiveRecord.php
  3. 22
      extensions/mongo/file/Collection.php
  4. 8
      tests/unit/extensions/mongo/file/CollectionTest.php
  5. 18
      tests/unit/extensions/mongo/file/QueryTest.php

4
extensions/mongo/Database.php

@ -34,7 +34,7 @@ class Database extends Object
/** /**
* Returns the Mongo collection with the given name. * Returns the Mongo collection with the given name.
* @param string $name collection 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. * @return Collection mongo collection instance.
*/ */
public function getCollection($name, $refresh = false) public function getCollection($name, $refresh = false)
@ -48,7 +48,7 @@ class Database extends Object
/** /**
* Returns Mongo GridFS collection with given prefix. * Returns Mongo GridFS collection with given prefix.
* @param string $prefix collection 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. * @return file\Collection mongo GridFS collection.
*/ */
public function getFileCollection($prefix = 'fs', $refresh = false) public function getFileCollection($prefix = 'fs', $refresh = false)

30
extensions/mongo/file/ActiveRecord.php

@ -16,6 +16,11 @@ namespace yii\mongo\file;
class ActiveRecord extends \yii\mongo\ActiveRecord class ActiveRecord extends \yii\mongo\ActiveRecord
{ {
/** /**
* @var \MongoGridFSFile|string
*/
public $file;
/**
* Creates an [[ActiveQuery]] instance. * Creates an [[ActiveQuery]] instance.
* This method is called by [[find()]] to start a "find" command. * 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 * 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. * Returns the list of all attribute names of the model.
* This method could be overridden by child classes to define available attributes. * This method could be overridden by child classes to define available attributes.
* Note: primary key attribute "_id" should be always present in returned array. * 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() public function attributes()
{ {
return ['id', 'file']; return ['id', 'filename'];
} }
/** /**

22
extensions/mongo/file/Collection.php

@ -8,10 +8,12 @@
namespace yii\mongo\file; namespace yii\mongo\file;
use yii\mongo\Exception; use yii\mongo\Exception;
use Yii;
/** /**
* Collection represents the Mongo GridFS collection information. * 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. * @method \MongoGridFSCursor find() returns a cursor for the search results.
* *
* @author Paul Klimov <klimov.paul@gmail.com> * @author Paul Klimov <klimov.paul@gmail.com>
@ -23,6 +25,26 @@ class Collection extends \yii\mongo\Collection
* @var \MongoGridFS Mongo GridFS collection instance. * @var \MongoGridFS Mongo GridFS collection instance.
*/ */
public $mongoCollection; 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. * Removes data from the collection.

8
tests/unit/extensions/mongo/file/CollectionTest.php

@ -17,6 +17,14 @@ class CollectionTest extends MongoTestCase
// Tests : // 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() public function testFind()
{ {
$collection = $this->getConnection()->getFileCollection(); $collection = $this->getConnection()->getFileCollection();

18
tests/unit/extensions/mongo/file/QueryTest.php

@ -29,7 +29,10 @@ class QueryTest extends MongoTestCase
{ {
$collection = $this->getConnection()->getFileCollection(); $collection = $this->getConnection()->getFileCollection();
for ($i = 1; $i <= 10; $i++) { 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); $row = $query->from('fs')->one($connection);
$this->assertTrue($row instanceof \MongoGridFSFile); $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']);
}
} }
Loading…
Cancel
Save