Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
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
sinan
laravel-mongodb
Commits
b3497f2b
Commit
b3497f2b
authored
Aug 15, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for push, fixes #20
parent
b115d728
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
10 deletions
+80
-10
README.md
README.md
+9
-0
Builder.php
src/Jenssegers/Mongodb/Builder.php
+44
-10
QueryTest.php
tests/QueryTest.php
+27
-0
No files found.
README.md
View file @
b3497f2b
...
...
@@ -243,6 +243,15 @@ Or you can access the internal object directly:
User::raw()->find();
### MongoDB specific operations
***Push**
*
Add one or more items to an array.
User::where('name', 'John')->push('items', 'boots');
User::where('name', 'John')->push('items', array('sword', 'shield'));
### Query Caching
You may easily cache the results of a query using the remember method:
...
...
src/Jenssegers/Mongodb/Builder.php
View file @
b3497f2b
...
...
@@ -303,16 +303,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
public
function
update
(
array
$values
)
{
$update
=
array
(
'$set'
=>
$values
);
$result
=
$this
->
collection
->
update
(
$this
->
compileWheres
(),
$update
,
array
(
'multiple'
=>
true
));
if
(
1
==
(
int
)
$result
[
'ok'
])
{
return
$result
[
'n'
];
}
return
0
;
return
$this
->
performUpdate
(
array
(
'$set'
=>
$values
));
}
/**
...
...
@@ -420,6 +411,31 @@ class Builder extends \Illuminate\Database\Query\Builder {
return
$this
->
collection
;
}
/**
* Append a value to an array.
*
* @param string $column
* @param mixed $value
* @return int
*/
public
function
push
(
$column
,
$value
=
null
)
{
if
(
is_array
(
$column
))
{
$query
=
array
(
'$push'
=>
$column
);
}
else
if
(
is_array
(
$value
))
{
$query
=
array
(
'$push'
=>
array
(
$column
=>
array
(
'$each'
=>
$value
)));
}
else
{
$query
=
array
(
'$push'
=>
array
(
$column
=>
$value
));
}
return
$this
->
performUpdate
(
$query
);
}
/**
* Get a new instance of the query builder.
*
...
...
@@ -430,6 +446,24 @@ class Builder extends \Illuminate\Database\Query\Builder {
return
new
Builder
(
$this
->
connection
);
}
/**
* Perform update.
*
* @param array $query
* @return int
*/
protected
function
performUpdate
(
$query
)
{
$result
=
$this
->
collection
->
update
(
$this
->
compileWheres
(),
$query
,
array
(
'multiple'
=>
true
));
if
(
1
==
(
int
)
$result
[
'ok'
])
{
return
$result
[
'n'
];
}
return
0
;
}
/**
* Compile the where array
*
...
...
tests/QueryTest.php
View file @
b3497f2b
...
...
@@ -101,4 +101,31 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this
->
assertInstanceOf
(
'MongoCollection'
,
$collection
);
}
public
function
testPush
()
{
$user
=
array
(
'name'
=>
'John Doe'
,
'tags'
=>
array
());
$id
=
DB
::
collection
(
'users'
)
->
insertGetId
(
$user
);
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
push
(
'tags'
,
'tag1'
);
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
1
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag1'
,
$user
[
'tags'
][
0
]);
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
push
(
'tags'
,
'tag2'
);
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
2
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag2'
,
$user
[
'tags'
][
1
]);
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
push
(
'tags'
,
array
(
'tag3'
,
'tag4'
));
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
4
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag4'
,
$user
[
'tags'
][
3
]);
}
}
\ 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