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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
85a32bea
Commit
85a32bea
authored
Dec 04, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Method "getChunkCollection()" added to Mongo file collection.
Method file unit tests improved.
parent
f0e62971
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
79 additions
and
4 deletions
+79
-4
Database.php
extensions/mongo/Database.php
+2
-2
ActiveRecord.php
extensions/mongo/file/ActiveRecord.php
+29
-1
Collection.php
extensions/mongo/file/Collection.php
+22
-0
CollectionTest.php
tests/unit/extensions/mongo/file/CollectionTest.php
+8
-0
QueryTest.php
tests/unit/extensions/mongo/file/QueryTest.php
+18
-1
No files found.
extensions/mongo/Database.php
View file @
85a32bea
...
@@ -34,7 +34,7 @@ class Database extends Object
...
@@ -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
...
@@ -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
)
...
...
extensions/mongo/file/ActiveRecord.php
View file @
85a32bea
...
@@ -16,6 +16,11 @@ namespace yii\mongo\file;
...
@@ -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
...
@@ -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
...
@@ -56,7 +84,7 @@ class ActiveRecord extends \yii\mongo\ActiveRecord
*/
*/
public
function
attributes
()
public
function
attributes
()
{
{
return
[
'id'
,
'file'
];
return
[
'id'
,
'file
name
'
];
}
}
/**
/**
...
...
extensions/mongo/file/Collection.php
View file @
85a32bea
...
@@ -8,10 +8,12 @@
...
@@ -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
...
@@ -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.
...
...
tests/unit/extensions/mongo/file/CollectionTest.php
View file @
85a32bea
...
@@ -17,6 +17,14 @@ class CollectionTest extends MongoTestCase
...
@@ -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
();
...
...
tests/unit/extensions/mongo/file/QueryTest.php
View file @
85a32bea
...
@@ -29,7 +29,10 @@ class QueryTest extends MongoTestCase
...
@@ -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
...
@@ -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'
]);
}
}
}
\ 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