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
d42a942a
Commit
d42a942a
authored
Nov 27, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mongo Command 'update' and 'insertBatch' methods added.
parent
4f5f5bb6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
98 additions
and
2 deletions
+98
-2
Collection.php
extensions/mongo/Collection.php
+45
-0
Query.php
extensions/mongo/Query.php
+1
-1
CollectionTest.php
tests/unit/extensions/mongo/CollectionTest.php
+52
-1
No files found.
extensions/mongo/Collection.php
View file @
d42a942a
...
@@ -79,6 +79,51 @@ class Collection extends Object
...
@@ -79,6 +79,51 @@ class Collection extends Object
}
}
/**
/**
* Inserts several new rows into collection.
* @param array $rows array of arrays or objects to be inserted.
* @param array $options list of options in format: optionName => optionValue.
* @return array inserted data, each row will have "_id" key assigned to it.
* @throws Exception on failure.
*/
public
function
batchInsert
(
$rows
,
$options
=
[])
{
$token
=
'Inserting batch data into '
.
$this
->
mongoCollection
->
getName
();
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$this
->
tryResultError
(
$this
->
mongoCollection
->
batchInsert
(
$rows
,
$options
));
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
$rows
;
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
throw
new
Exception
(
$e
->
getMessage
(),
(
int
)
$e
->
getCode
(),
$e
);
}
}
/**
* Updates the rows, which matches given criteria by given data.
* @param array $criteria description of the objects to update.
* @param array $newData the object with which to update the matching records.
* @param array $options list of options in format: optionName => optionValue.
* @return boolean whether operation was successful.
* @throws Exception on failure.
*/
public
function
update
(
$criteria
,
$newData
,
$options
=
[])
{
$token
=
'Updating data in '
.
$this
->
mongoCollection
->
getName
();
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$this
->
mongoCollection
->
update
(
$criteria
,
$newData
,
$options
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
true
;
}
catch
(
\Exception
$e
)
{
Yii
::
endProfile
(
$token
,
__METHOD__
);
throw
new
Exception
(
$e
->
getMessage
(),
(
int
)
$e
->
getCode
(),
$e
);
}
}
/**
* Update the existing database data, otherwise insert this data
* Update the existing database data, otherwise insert this data
* @param array|object $data data to be updated/inserted.
* @param array|object $data data to be updated/inserted.
* @param array $options list of options in format: optionName => optionValue.
* @param array $options list of options in format: optionName => optionValue.
...
...
extensions/mongo/Query.php
View file @
d42a942a
...
@@ -91,7 +91,7 @@ class Query extends Component implements QueryInterface
...
@@ -91,7 +91,7 @@ class Query extends Component implements QueryInterface
if
(
!
empty
(
$this
->
orderBy
))
{
if
(
!
empty
(
$this
->
orderBy
))
{
$sort
=
[];
$sort
=
[];
foreach
(
$this
->
orderBy
as
$fieldName
=>
$sortOrder
)
{
foreach
(
$this
->
orderBy
as
$fieldName
=>
$sortOrder
)
{
$sort
[
$fieldName
]
=
$sortOrder
===
SORT_DESC
?
-
1
:
1
;
$sort
[
$fieldName
]
=
$sortOrder
===
SORT_DESC
?
\MongoCollection
::
DESCENDING
:
\MongoCollection
::
ASCENDING
;
}
}
$cursor
->
sort
(
$this
->
orderBy
);
$cursor
->
sort
(
$this
->
orderBy
);
}
}
...
...
tests/unit/extensions/mongo/CollectionTest.php
View file @
d42a942a
...
@@ -15,6 +15,13 @@ class CollectionTest extends MongoTestCase
...
@@ -15,6 +15,13 @@ class CollectionTest extends MongoTestCase
// Tests :
// Tests :
public
function
testFind
()
{
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$cursor
=
$collection
->
find
();
$this
->
assertTrue
(
$cursor
instanceof
\MongoCursor
);
}
public
function
testInsert
()
public
function
testInsert
()
{
{
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
...
@@ -44,6 +51,28 @@ class CollectionTest extends MongoTestCase
...
@@ -44,6 +51,28 @@ class CollectionTest extends MongoTestCase
$this
->
assertEquals
(
$id
,
$rows
[
0
][
'_id'
]);
$this
->
assertEquals
(
$id
,
$rows
[
0
][
'_id'
]);
}
}
/**
* @depends testFind
*/
public
function
testBatchInsert
()
{
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$rows
=
[
[
'name'
=>
'customer 1'
,
'address'
=>
'customer 1 address'
,
],
[
'name'
=>
'customer 2'
,
'address'
=>
'customer 2 address'
,
],
];
$insertedRows
=
$collection
->
batchInsert
(
$rows
);
$this
->
assertTrue
(
$insertedRows
[
0
][
'_id'
]
instanceof
\MongoId
);
$this
->
assertTrue
(
$insertedRows
[
1
][
'_id'
]
instanceof
\MongoId
);
$this
->
assertEquals
(
count
(
$rows
),
$collection
->
find
()
->
count
());
}
public
function
testSave
()
public
function
testSave
()
{
{
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
...
@@ -59,7 +88,7 @@ class CollectionTest extends MongoTestCase
...
@@ -59,7 +88,7 @@ class CollectionTest extends MongoTestCase
/**
/**
* @depends testSave
* @depends testSave
*/
*/
public
function
testUpdate
()
public
function
testUpdate
BySave
()
{
{
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$data
=
[
$data
=
[
...
@@ -93,4 +122,25 @@ class CollectionTest extends MongoTestCase
...
@@ -93,4 +122,25 @@ class CollectionTest extends MongoTestCase
$rows
=
$collection
->
findAll
();
$rows
=
$collection
->
findAll
();
$this
->
assertEquals
(
0
,
count
(
$rows
));
$this
->
assertEquals
(
0
,
count
(
$rows
));
}
}
/**
* @depends testFindAll
*/
public
function
testUpdate
()
{
$collection
=
$this
->
getConnection
()
->
getCollection
(
'customer'
);
$data
=
[
'name'
=>
'customer 1'
,
'address'
=>
'customer 1 address'
,
];
$id
=
$collection
->
insert
(
$data
);
$newData
=
[
'name'
=>
'new name'
];
$collection
->
update
([
'_id'
=>
$id
],
$newData
);
list
(
$row
)
=
$collection
->
findAll
();
$this
->
assertEquals
(
$newData
[
'name'
],
$row
[
'name'
]);
}
}
}
\ 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