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
a39b2d37
Commit
a39b2d37
authored
Nov 29, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Default options setup added to Mongo Collection operations.
parent
9c7d2b23
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
13 deletions
+19
-13
ActiveRecord.php
extensions/mongo/ActiveRecord.php
+2
-10
Collection.php
extensions/mongo/Collection.php
+13
-1
CollectionTest.php
tests/unit/extensions/mongo/CollectionTest.php
+4
-2
No files found.
extensions/mongo/ActiveRecord.php
View file @
a39b2d37
...
...
@@ -134,10 +134,6 @@ abstract class ActiveRecord extends Model
*/
public
static
function
updateAll
(
$attributes
,
$condition
=
[],
$options
=
[])
{
$options
[
'w'
]
=
1
;
if
(
!
array_key_exists
(
'multiple'
,
$options
))
{
$options
[
'multiple'
]
=
true
;
}
return
static
::
getCollection
()
->
update
(
$condition
,
$attributes
,
$options
);
}
...
...
@@ -158,10 +154,6 @@ abstract class ActiveRecord extends Model
*/
public
static
function
updateAllCounters
(
$counters
,
$condition
=
[],
$options
=
[])
{
$options
[
'w'
]
=
1
;
if
(
!
array_key_exists
(
'multiple'
,
$options
))
{
$options
[
'multiple'
]
=
true
;
}
return
static
::
getCollection
()
->
update
(
$condition
,
[
'$inc'
=>
$counters
],
$options
);
}
...
...
@@ -798,7 +790,7 @@ abstract class ActiveRecord extends Model
}
// We do not check the return value of update() because it's possible
// that it doesn't change anything and thus returns 0.
$rows
=
static
::
getCollection
()
->
update
(
$condition
,
$values
,
[
'w'
=>
1
]
);
$rows
=
static
::
getCollection
()
->
update
(
$condition
,
$values
);
if
(
$lock
!==
null
&&
!
$rows
)
{
throw
new
StaleObjectException
(
'The object being updated is outdated.'
);
...
...
@@ -871,7 +863,7 @@ abstract class ActiveRecord extends Model
if
(
$lock
!==
null
)
{
$condition
[
$lock
]
=
$this
->
$lock
;
}
$result
=
static
::
getCollection
()
->
remove
(
$condition
,
[
'w'
=>
1
]
);
$result
=
static
::
getCollection
()
->
remove
(
$condition
);
if
(
$lock
!==
null
&&
!
$result
)
{
throw
new
StaleObjectException
(
'The object being deleted is outdated.'
);
}
...
...
extensions/mongo/Collection.php
View file @
a39b2d37
...
...
@@ -70,6 +70,7 @@ class Collection extends Object
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$options
=
array_merge
([
'w'
=>
1
],
$options
);
$this
->
tryResultError
(
$this
->
mongoCollection
->
insert
(
$data
,
$options
));
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
is_array
(
$data
)
?
$data
[
'_id'
]
:
$data
->
_id
;
...
...
@@ -92,6 +93,7 @@ class Collection extends Object
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$options
=
array_merge
([
'w'
=>
1
],
$options
);
$this
->
tryResultError
(
$this
->
mongoCollection
->
batchInsert
(
$rows
,
$options
));
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
$rows
;
...
...
@@ -115,7 +117,15 @@ class Collection extends Object
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$result
=
$this
->
mongoCollection
->
update
(
$this
->
buildCondition
(
$condition
),
$newData
,
$options
);
$options
=
array_merge
([
'w'
=>
1
,
'multiple'
=>
true
],
$options
);
if
(
$options
[
'multiple'
])
{
$keys
=
array_keys
(
$newData
);
if
(
!
empty
(
$keys
)
&&
strncmp
(
'$'
,
$keys
[
0
],
1
)
!==
0
)
{
$newData
=
[
'$set'
=>
$newData
];
}
}
$condition
=
$this
->
buildCondition
(
$condition
);
$result
=
$this
->
mongoCollection
->
update
(
$condition
,
$newData
,
$options
);
$this
->
tryResultError
(
$result
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
if
(
is_array
(
$result
)
&&
array_key_exists
(
'n'
,
$result
))
{
...
...
@@ -142,6 +152,7 @@ class Collection extends Object
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$options
=
array_merge
([
'w'
=>
1
],
$options
);
$this
->
tryResultError
(
$this
->
mongoCollection
->
save
(
$data
,
$options
));
Yii
::
endProfile
(
$token
,
__METHOD__
);
return
is_array
(
$data
)
?
$data
[
'_id'
]
:
$data
->
_id
;
...
...
@@ -164,6 +175,7 @@ class Collection extends Object
Yii
::
info
(
$token
,
__METHOD__
);
try
{
Yii
::
beginProfile
(
$token
,
__METHOD__
);
$options
=
array_merge
([
'w'
=>
1
,
'multiple'
=>
true
],
$options
);
$result
=
$this
->
mongoCollection
->
remove
(
$this
->
buildCondition
(
$condition
),
$options
);
$this
->
tryResultError
(
$result
);
Yii
::
endProfile
(
$token
,
__METHOD__
);
...
...
tests/unit/extensions/mongo/CollectionTest.php
View file @
a39b2d37
...
...
@@ -117,7 +117,8 @@ class CollectionTest extends MongoTestCase
];
$id
=
$collection
->
insert
(
$data
);
$collection
->
remove
([
'_id'
=>
$id
]);
$count
=
$collection
->
remove
([
'_id'
=>
$id
]);
$this
->
assertEquals
(
1
,
$count
);
$rows
=
$collection
->
findAll
();
$this
->
assertEquals
(
0
,
count
(
$rows
));
...
...
@@ -138,7 +139,8 @@ class CollectionTest extends MongoTestCase
$newData
=
[
'name'
=>
'new name'
];
$collection
->
update
([
'_id'
=>
$id
],
$newData
);
$count
=
$collection
->
update
([
'_id'
=>
$id
],
$newData
);
$this
->
assertEquals
(
1
,
$count
);
list
(
$row
)
=
$collection
->
findAll
();
$this
->
assertEquals
(
$newData
[
'name'
],
$row
[
'name'
]);
...
...
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