Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
16d857df
Commit
16d857df
authored
Dec 04, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mongo File Collection added.
parent
156f7c04
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
322 additions
and
2 deletions
+322
-2
Collection.php
extensions/mongo/Collection.php
+18
-0
Connection.php
extensions/mongo/Connection.php
+24
-2
Database.php
extensions/mongo/Database.php
+31
-0
Collection.php
extensions/mongo/file/Collection.php
+110
-0
ConnectionTest.php
tests/unit/extensions/mongo/ConnectionTest.php
+19
-0
DatabaseTest.php
tests/unit/extensions/mongo/DatabaseTest.php
+17
-0
MongoTestCase.php
tests/unit/extensions/mongo/MongoTestCase.php
+15
-0
CollectionTest.php
tests/unit/extensions/mongo/file/CollectionTest.php
+88
-0
No files found.
extensions/mongo/Collection.php
View file @
16d857df
...
...
@@ -59,6 +59,7 @@ use Yii;
*
* @property string $name name of this collection. This property is read-only.
* @property string $fullName full name of this collection, including database name. This property is read-only.
* @property array $lastError last error information. This property is read-only.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
...
...
@@ -87,6 +88,14 @@ class Collection extends Object
}
/**
* @return array last error information.
*/
public
function
getLastError
()
{
return
$this
->
mongoCollection
->
db
->
lastError
();
}
/**
* Drops this collection.
* @throws Exception on failure.
* @return boolean whether the operation successful.
...
...
@@ -553,6 +562,15 @@ class Collection extends Object
}
/**
* Throws an exception if there was an error on the last operation.
* @throws Exception if an error occurred.
*/
protected
function
tryLastError
()
{
$this
->
tryResultError
(
$this
->
getLastError
());
}
/**
* Converts user friendly condition keyword into actual Mongo condition keyword.
* @param string $key raw condition key.
* @return string actual key.
...
...
extensions/mongo/Connection.php
View file @
16d857df
...
...
@@ -162,10 +162,10 @@ class Connection extends Component
/**
* Returns the Mongo collection with the given name.
* @param string|array $name collection name. If string considered as
the name of the collection
* @param string|array $name collection name. If string considered as the name of the collection
* inside the default database. If array - first element considered as the name of the database,
* second - as name of collection inside that database
* @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
)
...
...
@@ -179,6 +179,28 @@ class Connection extends Component
}
/**
* Returns the Mongo GridFS collection.
* @param string|array $prefix collection prefix. If string considered as the prefix of the GridFS
* collection inside the default database. If array - first element considered as the name of the database,
* second - as prefix of the GridFS collection inside that database, if no second element present
* default "fs" prefix will be used.
* @param boolean $refresh whether to reload the collection instance even if it is found in the cache.
* @return file\Collection Mongo GridFS collection instance.
*/
public
function
getFileCollection
(
$prefix
=
'fs'
,
$refresh
=
false
)
{
if
(
is_array
(
$prefix
))
{
list
(
$dbName
,
$collectionPrefix
)
=
$prefix
;
if
(
!
isset
(
$collectionPrefix
))
{
$collectionPrefix
=
'fs'
;
}
return
$this
->
getDatabase
(
$dbName
)
->
getFileCollection
(
$collectionPrefix
,
$refresh
);
}
else
{
return
$this
->
getDatabase
()
->
getFileCollection
(
$prefix
,
$refresh
);
}
}
/**
* Returns a value indicating whether the Mongo connection is established.
* @return boolean whether the Mongo connection is established
*/
...
...
extensions/mongo/Database.php
View file @
16d857df
...
...
@@ -26,6 +26,10 @@ class Database extends Object
* @var Collection[] list of collections.
*/
private
$_collections
=
[];
/**
* @var file\Collection[] list of GridFS collections.
*/
private
$_fileCollections
=
[];
/**
* Returns the Mongo collection with the given name.
...
...
@@ -42,6 +46,20 @@ 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.
* @return file\Collection mongo GridFS collection.
*/
public
function
getFileCollection
(
$prefix
=
'fs'
,
$refresh
=
false
)
{
if
(
$refresh
||
!
array_key_exists
(
$prefix
,
$this
->
_fileCollections
))
{
$this
->
_fileCollections
[
$prefix
]
=
$this
->
selectFileCollection
(
$prefix
);
}
return
$this
->
_fileCollections
[
$prefix
];
}
/**
* Selects collection with given name.
* @param string $name collection name.
* @return Collection collection instance.
...
...
@@ -55,6 +73,19 @@ class Database extends Object
}
/**
* Selects GridFS collection with given prefix.
* @param string $prefix file collection prefix.
* @return file\Collection file collection instance.
*/
protected
function
selectFileCollection
(
$prefix
)
{
return
Yii
::
createObject
([
'class'
=>
'yii\mongo\file\Collection'
,
'mongoCollection'
=>
$this
->
mongoDb
->
getGridFS
(
$prefix
)
]);
}
/**
* Creates new collection.
* Note: Mongo creates new collections automatically on the first demand,
* this method makes sense only for the migration script or for the case
...
...
extensions/mongo/file/Collection.php
0 → 100644
View file @
16d857df
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\mongo\file
;
use
yii\mongo\Exception
;
/**
* Collection represents the Mongo GridFS collection information.
*
* @method \MongoGridFSCursor find() returns a cursor for the search results.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class
Collection
extends
\yii\mongo\Collection
{
/**
* @var \MongoGridFS Mongo GridFS collection instance.
*/
public
$mongoCollection
;
/**
* Removes data from the collection.
* @param array $condition description of records to remove.
* @param array $options list of options in format: optionName => optionValue.
* @return integer|boolean number of updated documents or whether operation was successful.
* @throws Exception on failure.
*/
public
function
remove
(
$condition
=
[],
$options
=
[])
{
$result
=
parent
::
remove
(
$condition
,
$options
);
$this
->
tryLastError
();
// MongoGridFS::remove will return even if the remove failed
return
$result
;
}
/**
* @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
=
[])
{
$options
=
array_merge
([
'w'
=>
1
],
$options
);
return
$this
->
mongoCollection
->
storeBytes
(
$bytes
,
$metadata
,
$options
);
}
/**
* @param string $filename name of the file 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
=
[])
{
$options
=
array_merge
([
'w'
=>
1
],
$options
);
return
$this
->
mongoCollection
->
storeFile
(
$filename
,
$metadata
,
$options
);
}
/**
* @param string $name name of the uploaded file to store. This should correspond to
* the file field's name attribute in the HTML form.
* @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
storeUploads
(
$name
,
$metadata
=
[])
{
return
$this
->
mongoCollection
->
storeUpload
(
$name
,
$metadata
);
}
/**
* @param mixed $id _id of the file to find.
* @return \MongoGridFSFile|null found file, or null if file does not exist
*/
public
function
get
(
$id
)
{
return
$this
->
mongoCollection
->
get
(
$id
);
}
/**
* @param mixed $id _id of the file to find.
* @return boolean whether the operation was successful.
*/
public
function
delete
(
$id
)
{
$result
=
$this
->
mongoCollection
->
delete
(
$id
);
$this
->
tryResultError
(
$result
);
return
true
;
}
}
\ No newline at end of file
tests/unit/extensions/mongo/ConnectionTest.php
View file @
16d857df
...
...
@@ -3,6 +3,7 @@
namespace
yiiunit\extensions\mongo
;
use
yii\mongo\Collection
;
use
yii\mongo\file\Collection
as
FileCollection
;
use
yii\mongo\Connection
;
use
yii\mongo\Database
;
...
...
@@ -98,4 +99,21 @@ class ConnectionTest extends MongoTestCase
$collection2
=
$connection
->
getCollection
(
'customer'
,
true
);
$this
->
assertFalse
(
$collection
===
$collection2
);
}
/**
* @depends testGetDefaultDatabase
*/
public
function
testGetFileCollection
()
{
$connection
=
$this
->
getConnection
();
$collection
=
$connection
->
getFileCollection
(
'testfs'
);
$this
->
assertTrue
(
$collection
instanceof
FileCollection
);
$collection2
=
$connection
->
getFileCollection
(
'testfs'
);
$this
->
assertTrue
(
$collection
===
$collection2
);
$collection2
=
$connection
->
getFileCollection
(
'testfs'
,
true
);
$this
->
assertFalse
(
$collection
===
$collection2
);
}
}
\ No newline at end of file
tests/unit/extensions/mongo/DatabaseTest.php
View file @
16d857df
...
...
@@ -3,6 +3,7 @@
namespace
yiiunit\extensions\mongo
;
use
yii\mongo\Collection
;
use
yii\mongo\file\Collection
as
FileCollection
;
/**
* @group mongo
...
...
@@ -12,6 +13,7 @@ class DatabaseTest extends MongoTestCase
protected
function
tearDown
()
{
$this
->
dropCollection
(
'customer'
);
$this
->
dropFileCollection
(
'testfs'
);
parent
::
tearDown
();
}
...
...
@@ -32,6 +34,21 @@ class DatabaseTest extends MongoTestCase
$this
->
assertFalse
(
$collection
===
$collectionRefreshed
);
}
public
function
testGetFileCollection
()
{
$database
=
$connection
=
$this
->
getConnection
()
->
getDatabase
();
$collection
=
$database
->
getFileCollection
(
'testfs'
);
$this
->
assertTrue
(
$collection
instanceof
FileCollection
);
$this
->
assertTrue
(
$collection
->
mongoCollection
instanceof
\MongoGridFS
);
$collection2
=
$database
->
getFileCollection
(
'testfs'
);
$this
->
assertTrue
(
$collection
===
$collection2
);
$collectionRefreshed
=
$database
->
getFileCollection
(
'testfs'
,
true
);
$this
->
assertFalse
(
$collection
===
$collectionRefreshed
);
}
public
function
testCommand
()
{
$database
=
$connection
=
$this
->
getConnection
()
->
getDatabase
();
...
...
tests/unit/extensions/mongo/MongoTestCase.php
View file @
16d857df
...
...
@@ -105,6 +105,21 @@ class MongoTestCase extends TestCase
}
/**
* Drops the specified file collection.
* @param string $name file collection name.
*/
protected
function
dropFileCollection
(
$name
)
{
if
(
$this
->
mongo
)
{
try
{
$this
->
mongo
->
getFileCollection
(
$name
)
->
drop
();
}
catch
(
Exception
$e
)
{
// shut down exception
}
}
}
/**
* Finds all records in collection.
* @param \yii\mongo\Collection $collection
* @param array $condition
...
...
tests/unit/extensions/mongo/file/CollectionTest.php
0 → 100644
View file @
16d857df
<?php
namespace
yiiunit\extensions\mongo\file
;
use
yiiunit\extensions\mongo\MongoTestCase
;
class
CollectionTest
extends
MongoTestCase
{
protected
function
tearDown
()
{
$this
->
dropFileCollection
(
'fs'
);
parent
::
tearDown
();
}
// Tests :
public
function
testFind
()
{
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$cursor
=
$collection
->
find
();
$this
->
assertTrue
(
$cursor
instanceof
\MongoGridFSCursor
);
}
public
function
testStoreFile
()
{
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$filename
=
__FILE__
;
$id
=
$collection
->
storeFile
(
$filename
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$files
=
$this
->
findAll
(
$collection
);
$this
->
assertEquals
(
1
,
count
(
$files
));
/** @var $file \MongoGridFSFile */
$file
=
$files
[
0
];
$this
->
assertEquals
(
$filename
,
$file
->
getFilename
());
$this
->
assertEquals
(
file_get_contents
(
$filename
),
$file
->
getBytes
());
}
public
function
testStoreBytes
()
{
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$bytes
=
'Test file content'
;
$id
=
$collection
->
storeBytes
(
$bytes
);
$this
->
assertTrue
(
$id
instanceof
\MongoId
);
$files
=
$this
->
findAll
(
$collection
);
$this
->
assertEquals
(
1
,
count
(
$files
));
/** @var $file \MongoGridFSFile */
$file
=
$files
[
0
];
$this
->
assertEquals
(
$bytes
,
$file
->
getBytes
());
}
/**
* @depends testStoreBytes
*/
public
function
testGet
()
{
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$bytes
=
'Test file content'
;
$id
=
$collection
->
storeBytes
(
$bytes
);
$file
=
$collection
->
get
(
$id
);
$this
->
assertTrue
(
$file
instanceof
\MongoGridFSFile
);
$this
->
assertEquals
(
$bytes
,
$file
->
getBytes
());
}
/**
* @depends testGet
*/
public
function
testDelete
()
{
$collection
=
$this
->
getConnection
()
->
getFileCollection
();
$bytes
=
'Test file content'
;
$id
=
$collection
->
storeBytes
(
$bytes
);
$this
->
assertTrue
(
$collection
->
delete
(
$id
));
$file
=
$collection
->
get
(
$id
);
$this
->
assertNull
(
$file
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment