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
ac1bd369
Commit
ac1bd369
authored
Sep 09, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added , fixes #34
parent
5b93d89b
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
158 additions
and
31 deletions
+158
-31
README.md
README.md
+11
-0
Builder.php
src/Jenssegers/Mongodb/Builder.php
+61
-31
Model.php
src/Jenssegers/Mongodb/Model.php
+37
-0
ModelTest.php
tests/ModelTest.php
+27
-0
QueryBuilderTest.php
tests/QueryBuilderTest.php
+22
-0
No files found.
README.md
View file @
ac1bd369
...
...
@@ -288,6 +288,17 @@ Remove one or more values from an array.
DB::collection('users')->where('name', 'John')->pull('items', 'boots');
DB::collection('users')->where('name', 'John')->pull('items', array('sword', 'shield'));
**Unset**
Remove one or more fields from a document.
DB::collection('users')->where('name', 'John')->unset('note');
You can also perform an unset on a model.
$user = User::where('name', 'John')->first();
$user->unset('note');
### Query Caching
You may easily cache the results of a query using the remember method:
...
...
src/Jenssegers/Mongodb/Builder.php
View file @
ac1bd369
...
...
@@ -7,17 +7,17 @@ use Closure;
class
Builder
extends
\Illuminate\Database\Query\Builder
{
/**
* The database collection
*
* @var MongoCollection
*/
* The database collection
*
* @var MongoCollection
*/
protected
$collection
;
/**
* All of the available operators.
*
* @var array
*/
* All of the available operators.
*
* @var array
*/
protected
$conversion
=
array
(
'='
=>
'='
,
'!='
=>
'$ne'
,
...
...
@@ -29,11 +29,11 @@ class Builder extends \Illuminate\Database\Query\Builder {
);
/**
* Create a new query builder instance.
*
* @param Connection $connection
* @return void
*/
* Create a new query builder instance.
*
* @param Connection $connection
* @return void
*/
public
function
__construct
(
Connection
$connection
)
{
$this
->
connection
=
$connection
;
...
...
@@ -339,24 +339,15 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/
public
function
increment
(
$column
,
$amount
=
1
,
array
$extra
=
array
())
{
// build update statement
$update
=
array
(
$query
=
array
(
'$inc'
=>
array
(
$column
=>
$amount
),
'$set'
=>
$extra
,
);
//
p
rotect
//
P
rotect
$this
->
whereNotNull
(
$column
);
// perform
$result
=
$this
->
collection
->
update
(
$this
->
compileWheres
(),
$update
,
array
(
'multiple'
=>
true
));
if
(
1
==
(
int
)
$result
[
'ok'
])
{
return
$result
[
'n'
];
}
return
0
;
return
$this
->
performUpdate
(
$query
);
}
/**
...
...
@@ -505,6 +496,28 @@ class Builder extends \Illuminate\Database\Query\Builder {
return
$this
->
performUpdate
(
$query
);
}
/**
* Remove one or more fields.
*
* @param mixed $columns
* @return int
*/
public
function
dropColumn
(
$columns
)
{
if
(
!
is_array
(
$columns
))
$columns
=
array
(
$columns
);
$fields
=
array
();
foreach
(
$columns
as
$column
)
{
$fields
[
$column
]
=
1
;
}
$query
=
array
(
'$unset'
=>
$fields
);
return
$this
->
performUpdate
(
$query
);
}
/**
* Get a new instance of the query builder.
*
...
...
@@ -516,7 +529,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
/**
* Perform
update
.
* Perform
an update query
.
*
* @param array $query
* @param array $options
...
...
@@ -541,7 +554,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
/**
* Convert a key to MongoID if needed
* Convert a key to MongoID if needed
.
*
* @param mixed $id
* @return mixed
...
...
@@ -557,10 +570,10 @@ class Builder extends \Illuminate\Database\Query\Builder {
}
/**
* Compile the where array
*
* @return array
*/
* Compile the where array.
*
* @return array
*/
protected
function
compileWheres
()
{
if
(
!
$this
->
wheres
)
return
array
();
...
...
@@ -694,4 +707,21 @@ class Builder extends \Illuminate\Database\Query\Builder {
return
$where
[
'sql'
];
}
/**
* Handle dynamic method calls into the method.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public
function
__call
(
$method
,
$parameters
)
{
if
(
$method
==
'unset'
)
{
return
call_user_func_array
(
array
(
$this
,
'dropColumn'
),
$parameters
);
}
return
parent
::
__call
(
$method
,
$parameters
);
}
}
\ No newline at end of file
src/Jenssegers/Mongodb/Model.php
View file @
ac1bd369
...
...
@@ -213,4 +213,41 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
parent
::
setRawAttributes
(
$attributes
,
$sync
);
}
/**
* Remove one or more fields.
*
* @param mixed $columns
* @return int
*/
public
function
dropColumn
(
$columns
)
{
if
(
!
is_array
(
$columns
))
$columns
=
array
(
$columns
);
// Unset attributes
foreach
(
$columns
as
$column
)
{
$this
->
__unset
(
$column
);
}
// Perform unset only on current document
return
$query
=
$this
->
newQuery
()
->
where
(
$this
->
getKeyName
(),
$this
->
getKey
())
->
unset
(
$columns
);
}
/**
* Handle dynamic method calls into the method.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public
function
__call
(
$method
,
$parameters
)
{
if
(
$method
==
'unset'
)
{
return
call_user_func_array
(
array
(
$this
,
'dropColumn'
),
$parameters
);
}
return
parent
::
__call
(
$method
,
$parameters
);
}
}
\ No newline at end of file
tests/ModelTest.php
View file @
ac1bd369
...
...
@@ -278,4 +278,31 @@ class ModelTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
$original
[
0
],
$items
[
0
]
->
toArray
());
}
public
function
testUnset
()
{
$user1
=
User
::
create
(
array
(
'name'
=>
'John Doe'
,
'note1'
=>
'ABC'
,
'note2'
=>
'DEF'
));
$user2
=
User
::
create
(
array
(
'name'
=>
'Jane Doe'
,
'note1'
=>
'ABC'
,
'note2'
=>
'DEF'
));
$user1
->
unset
(
'note1'
);
$this
->
assertFalse
(
isset
(
$user1
->
note1
));
$this
->
assertTrue
(
isset
(
$user1
->
note2
));
$this
->
assertTrue
(
isset
(
$user2
->
note1
));
$this
->
assertTrue
(
isset
(
$user2
->
note2
));
// Re-fetch to be sure
$user1
=
User
::
find
(
$user1
->
_id
);
$user2
=
User
::
find
(
$user2
->
_id
);
$this
->
assertFalse
(
isset
(
$user1
->
note1
));
$this
->
assertTrue
(
isset
(
$user1
->
note2
));
$this
->
assertTrue
(
isset
(
$user2
->
note1
));
$this
->
assertTrue
(
isset
(
$user2
->
note2
));
$user2
->
unset
(
array
(
'note1'
,
'note2'
));
$this
->
assertFalse
(
isset
(
$user2
->
note1
));
$this
->
assertFalse
(
isset
(
$user2
->
note2
));
}
}
\ No newline at end of file
tests/QueryBuilderTest.php
View file @
ac1bd369
...
...
@@ -383,4 +383,26 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
1
,
DB
::
collection
(
'items'
)
->
count
());
}
public
function
testUnset
()
{
$id1
=
DB
::
collection
(
'users'
)
->
insertGetId
(
array
(
'name'
=>
'John Doe'
,
'note1'
=>
'ABC'
,
'note2'
=>
'DEF'
));
$id2
=
DB
::
collection
(
'users'
)
->
insertGetId
(
array
(
'name'
=>
'Jane Doe'
,
'note1'
=>
'ABC'
,
'note2'
=>
'DEF'
));
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'John Doe'
)
->
unset
(
'note1'
);
$user1
=
DB
::
collection
(
'users'
)
->
find
(
$id1
);
$user2
=
DB
::
collection
(
'users'
)
->
find
(
$id2
);
$this
->
assertFalse
(
isset
(
$user1
[
'note1'
]));
$this
->
assertTrue
(
isset
(
$user1
[
'note2'
]));
$this
->
assertTrue
(
isset
(
$user2
[
'note1'
]));
$this
->
assertTrue
(
isset
(
$user2
[
'note2'
]));
DB
::
collection
(
'users'
)
->
where
(
'name'
,
'Jane Doe'
)
->
unset
(
array
(
'note1'
,
'note2'
));
$user2
=
DB
::
collection
(
'users'
)
->
find
(
$id2
);
$this
->
assertFalse
(
isset
(
$user2
[
'note1'
]));
$this
->
assertFalse
(
isset
(
$user2
[
'note2'
]));
}
}
\ 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