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
36bf9a27
Commit
36bf9a27
authored
Jul 13, 2013
by
Hannes Van De Vreken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added whereNotIn, increment, decrement + tests + readme examples
parent
40b16e34
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
0 deletions
+127
-0
.travis.yml
.travis.yml
+1
-0
README.md
README.md
+26
-0
Builder.php
src/Jenssegers/Mongodb/Builder.php
+50
-0
QueryTest.php
tests/QueryTest.php
+50
-0
No files found.
.travis.yml
View file @
36bf9a27
...
@@ -7,6 +7,7 @@ branches:
...
@@ -7,6 +7,7 @@ branches:
php
:
php
:
-
5.3
-
5.3
-
5.4
-
5.4
-
5.5
services
:
mongodb
services
:
mongodb
...
...
README.md
View file @
36bf9a27
...
@@ -97,6 +97,8 @@ $users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
...
@@ -97,6 +97,8 @@ $users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
$users
=
User
::
whereIn
(
'age'
,
array
(
16
,
18
,
20
))
->
get
();
$users
=
User
::
whereIn
(
'age'
,
array
(
16
,
18
,
20
))
->
get
();
```
```
When using
`whereNotIn`
objects will be returned if the field is non existant. Combine with
`whereNotNull('age')`
to leave out those documents.
**Using Where Between**
**Using Where Between**
```
php
```
php
...
@@ -181,3 +183,27 @@ $user = Comment::where('body', 'like', '%spam%')->get();
...
@@ -181,3 +183,27 @@ $user = Comment::where('body', 'like', '%spam%')->get();
**Inserts, updates and deletes**
**Inserts, updates and deletes**
All basic insert, update, delete and select methods should be implemented.
All basic insert, update, delete and select methods should be implemented.
**Increments & decrements**
Perform increments (default 1) on specified attributes.
Attention: without a where-clause, every object will be modified.
The number of updated objects is returned.
```
php
User
::
where
(
'name'
,
'John Doe'
)
->
increment
(
'age'
);
User
::
where
(
'name'
,
'Bart De Wever'
)
->
decrement
(
'weight'
,
50
);
$count
=
User
->
increment
(
'age'
);
echo
$count
;
```
will return the number of users where
`age`
is a valid field.
These functions also allow for a third attribute:
```
php
User
::
where
(
'age'
,
'29'
)
->
increment
(
'age'
,
1
,
array
(
'group'
=>
'thirty something'
));
User
::
where
(
'bmi'
,
30
)
->
decrement
(
'bmi'
,
1
,
array
(
'category'
=>
'overweight'
));
```
\ No newline at end of file
src/Jenssegers/Mongodb/Builder.php
View file @
36bf9a27
...
@@ -286,6 +286,49 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -286,6 +286,49 @@ class Builder extends \Illuminate\Database\Query\Builder {
return
0
;
return
0
;
}
}
/**
* Increment a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public
function
increment
(
$column
,
$amount
=
1
,
array
$extra
=
array
())
{
// build update statement
$update
=
array
(
'$inc'
=>
array
(
$column
=>
$amount
),
'$set'
=>
$extra
,
);
// protect
$this
->
whereNotNull
(
$column
);
// perform
$result
=
$this
->
collection
->
update
(
$this
->
compileWheres
(),
$update
,
array
(
'multiple'
=>
true
));
if
(
1
==
(
int
)
$result
[
'ok'
])
{
return
$result
[
'n'
];
}
return
0
;
}
/**
* Decrement a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public
function
decrement
(
$column
,
$amount
=
1
,
array
$extra
=
array
())
{
return
$this
->
increment
(
$column
,
-
1
*
$amount
,
$extra
);
}
/**
/**
* Delete a record from the database.
* Delete a record from the database.
*
*
...
@@ -429,6 +472,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
...
@@ -429,6 +472,13 @@ class Builder extends \Illuminate\Database\Query\Builder {
return
array
(
$column
=>
array
(
'$in'
=>
$values
));
return
array
(
$column
=>
array
(
'$in'
=>
$values
));
}
}
private
function
compileWhereNotIn
(
$where
)
{
extract
(
$where
);
return
array
(
$column
=>
array
(
'$nin'
=>
$values
));
}
private
function
compileWhereNull
(
$where
)
private
function
compileWhereNull
(
$where
)
{
{
$where
[
'operator'
]
=
'='
;
$where
[
'operator'
]
=
'='
;
...
...
tests/QueryTest.php
View file @
36bf9a27
...
@@ -112,6 +112,12 @@ class QueryTest extends PHPUnit_Framework_TestCase {
...
@@ -112,6 +112,12 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$user
=
User
::
select
(
'name'
,
'title'
)
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'admin'
,
$user
->
title
);
$this
->
assertEquals
(
null
,
$user
->
age
);
$user
=
User
::
get
(
array
(
'name'
))
->
first
();
$user
=
User
::
get
(
array
(
'name'
))
->
first
();
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
$this
->
assertEquals
(
'John Doe'
,
$user
->
name
);
...
@@ -143,12 +149,22 @@ class QueryTest extends PHPUnit_Framework_TestCase {
...
@@ -143,12 +149,22 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$users
=
User
::
whereIn
(
'age'
,
array
(
33
,
35
,
13
))
->
get
();
$users
=
User
::
whereIn
(
'age'
,
array
(
33
,
35
,
13
))
->
get
();
$this
->
assertEquals
(
6
,
count
(
$users
));
$this
->
assertEquals
(
6
,
count
(
$users
));
$users
=
User
::
whereNotIn
(
'age'
,
array
(
33
,
35
))
->
get
();
$this
->
assertEquals
(
4
,
count
(
$users
));
$users
=
User
::
whereNotNull
(
'age'
)
->
whereNotIn
(
'age'
,
array
(
33
,
35
))
->
get
();
$this
->
assertEquals
(
3
,
count
(
$users
));
}
}
public
function
testWhereNull
()
public
function
testWhereNull
()
{
{
$users
=
User
::
whereNull
(
'age'
)
->
get
();
$users
=
User
::
whereNull
(
'age'
)
->
get
();
$this
->
assertEquals
(
1
,
count
(
$users
));
$this
->
assertEquals
(
1
,
count
(
$users
));
$users
=
User
::
whereNotNull
(
'age'
)
->
get
();
$this
->
assertEquals
(
8
,
count
(
$users
));
}
}
public
function
testOrder
()
public
function
testOrder
()
...
@@ -173,6 +189,37 @@ class QueryTest extends PHPUnit_Framework_TestCase {
...
@@ -173,6 +189,37 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'Jane Doe'
,
$users
[
0
]
->
name
);
$this
->
assertEquals
(
'Jane Doe'
,
$users
[
0
]
->
name
);
}
}
public
function
testIncrements
()
{
User
::
where
(
'name'
,
'John Doe'
)
->
increment
(
'age'
);
User
::
where
(
'name'
,
'John Doe'
)
->
increment
(
'age'
,
2
,
array
(
'title'
=>
'user'
));
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
first
();
$this
->
assertEquals
(
38
,
$user
->
age
);
$this
->
assertEquals
(
'user'
,
$user
->
title
);
User
::
where
(
'name'
,
'John Doe'
)
->
decrement
(
'age'
);
$num
=
User
::
where
(
'name'
,
'John Doe'
)
->
decrement
(
'age'
,
2
,
array
(
'title'
=>
'admin'
));
$user
=
User
::
where
(
'name'
,
'John Doe'
)
->
first
();
$this
->
assertEquals
(
35
,
$user
->
age
);
$this
->
assertEquals
(
'admin'
,
$user
->
title
);
$this
->
assertEquals
(
1
,
$num
);
User
::
increment
(
'age'
);
User
::
increment
(
'age'
,
2
);
$user
=
User
::
where
(
'name'
,
'Mark Moe'
)
->
first
();
$this
->
assertEquals
(
26
,
$user
->
age
);
User
::
decrement
(
'age'
,
2
);
$num
=
User
::
decrement
(
'age'
);
$user
=
User
::
where
(
'name'
,
'Mark Moe'
)
->
first
();
$this
->
assertEquals
(
23
,
$user
->
age
);
$this
->
assertEquals
(
8
,
$num
);
}
public
function
testAggregates
()
public
function
testAggregates
()
{
{
$this
->
assertEquals
(
9
,
User
::
count
());
$this
->
assertEquals
(
9
,
User
::
count
());
...
@@ -183,6 +230,9 @@ class QueryTest extends PHPUnit_Framework_TestCase {
...
@@ -183,6 +230,9 @@ class QueryTest extends PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
35
,
User
::
where
(
'title'
,
'admin'
)
->
max
(
'age'
));
$this
->
assertEquals
(
35
,
User
::
where
(
'title'
,
'admin'
)
->
max
(
'age'
));
$this
->
assertEquals
(
37
,
User
::
where
(
'title'
,
'user'
)
->
max
(
'age'
));
$this
->
assertEquals
(
37
,
User
::
where
(
'title'
,
'user'
)
->
max
(
'age'
));
$this
->
assertEquals
(
33
,
User
::
where
(
'title'
,
'admin'
)
->
min
(
'age'
));
$this
->
assertEquals
(
13
,
User
::
where
(
'title'
,
'user'
)
->
min
(
'age'
));
}
}
public
function
testGroupBy
()
public
function
testGroupBy
()
...
...
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