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
816de8df
Commit
816de8df
authored
Aug 15, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding pull support
parent
7471a0a7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
2 deletions
+55
-2
README.md
README.md
+7
-0
Builder.php
src/Jenssegers/Mongodb/Builder.php
+28
-2
QueryTest.php
tests/QueryTest.php
+20
-0
No files found.
README.md
View file @
816de8df
...
@@ -252,6 +252,13 @@ Add one or more items to an array.
...
@@ -252,6 +252,13 @@ Add one or more items to an array.
User::where('name', 'John')->push('items', 'boots');
User::where('name', 'John')->push('items', 'boots');
User::where('name', 'John')->push('items', array('sword', 'shield'));
User::where('name', 'John')->push('items', array('sword', 'shield'));
***Pull**
*
Remove one or more values from an array.
User::where('name', 'John')->pull('items', 'boots');
User::where('name', 'John')->pull('items', array('sword', 'shield'));
### Query Caching
### Query Caching
You may easily cache the results of a query using the remember method:
You may easily cache the results of a query using the remember method:
...
...
src/Jenssegers/Mongodb/Builder.php
View file @
816de8df
...
@@ -412,9 +412,9 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -412,9 +412,9 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
}
/**
/**
* Append
a value
to an array.
* Append
one or more values
to an array.
*
*
* @param
string
$column
* @param
mixed
$column
* @param mixed $value
* @param mixed $value
* @return int
* @return int
*/
*/
...
@@ -426,6 +426,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -426,6 +426,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
}
else
if
(
is_array
(
$value
))
else
if
(
is_array
(
$value
))
{
{
// $pushAll depricated
$query
=
array
(
'$push'
=>
array
(
$column
=>
array
(
'$each'
=>
$value
)));
$query
=
array
(
'$push'
=>
array
(
$column
=>
array
(
'$each'
=>
$value
)));
}
}
else
else
...
@@ -436,6 +437,31 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -436,6 +437,31 @@ class Builder extends \Illuminate\Database\Query\Builder {
return
$this
->
performUpdate
(
$query
);
return
$this
->
performUpdate
(
$query
);
}
}
/**
* Remove one or more values from an array.
*
* @param mixed $column
* @param mixed $value
* @return int
*/
public
function
pull
(
$column
,
$value
=
null
)
{
if
(
is_array
(
$column
))
{
$query
=
array
(
'$pull'
=>
$column
);
}
else
if
(
is_array
(
$value
))
{
$query
=
array
(
'$pullAll'
=>
array
(
$column
=>
$value
));
}
else
{
$query
=
array
(
'$pull'
=>
array
(
$column
=>
$value
));
}
return
$this
->
performUpdate
(
$query
);
}
/**
/**
* Get a new instance of the query builder.
* Get a new instance of the query builder.
*
*
...
...
tests/QueryTest.php
View file @
816de8df
...
@@ -128,4 +128,24 @@ class QueryTest extends PHPUnit_Framework_TestCase {
...
@@ -128,4 +128,24 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'tag4'
,
$user
[
'tags'
][
3
]);
$this
->
assertEquals
(
'tag4'
,
$user
[
'tags'
][
3
]);
}
}
public
function
testPull
()
{
$user
=
array
(
'name'
=>
'John Doe'
,
'tags'
=>
array
(
'tag1'
,
'tag2'
,
'tag3'
,
'tag4'
));
$id
=
DB
::
collection
(
'users'
)
->
insertGetId
(
$user
);
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
pull
(
'tags'
,
'tag3'
);
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
3
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag4'
,
$user
[
'tags'
][
2
]);
DB
::
collection
(
'users'
)
->
where
(
'_id'
,
$id
)
->
pull
(
'tags'
,
array
(
'tag2'
,
'tag4'
));
$user
=
DB
::
collection
(
'users'
)
->
find
(
$id
);
$this
->
assertTrue
(
is_array
(
$user
[
'tags'
]));
$this
->
assertEquals
(
1
,
count
(
$user
[
'tags'
]));
$this
->
assertEquals
(
'tag1'
,
$user
[
'tags'
][
0
]);
}
}
}
\ 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