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
3a6ad41b
Commit
3a6ad41b
authored
Apr 28, 2013
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Advanced queries and readme update
parent
f8054f1c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
19 deletions
+81
-19
README.md
README.md
+58
-4
Query.php
src/Jenssegers/Mongodb/Query.php
+23
-15
No files found.
README.md
View file @
3a6ad41b
Laravel Eloquent MongoDB
Laravel Eloquent MongoDB
========================
========================
This is an
alternative Eloquent model that supports MongoDB. The code structure is not great, and there is little functionality at this moment
.
This is an
Eloquent model that supports MongoDB
.
Some code is based on https://github.com/navruzm/lmongo, that has way more complete features, but works a bit different than Eloquent.
Installation
------------
Add the package to your
`composer.json`
or install manually.
{
"require": {
"navruzm/lmongo": "*"
}
}
Run
`composer update`
to download the package from packagist.
Add the service provider to your
`config/app.php`
:
'Jenssegers\Mongodb\MongodbServiceProvider'
Usage
Usage
-----
-----
...
@@ -21,7 +37,7 @@ Tell your model to use the MongoDB model and a MongoDB collection:
...
@@ -21,7 +37,7 @@ Tell your model to use the MongoDB model and a MongoDB collection:
Configuration
Configuration
-------------
-------------
The model will automatically check the Laravel database configuration array for a 'mongodb' item.
The model will automatically check the Laravel database configuration array
in
`config/database.php`
for a 'mongodb' item.
'mongodb' => array(
'mongodb' => array(
'host' => 'localhost',
'host' => 'localhost',
...
@@ -35,4 +51,42 @@ You can also specify the connection name in the model:
...
@@ -35,4 +51,42 @@ You can also specify the connection name in the model:
protected $connection = 'mongodb2';
protected $connection = 'mongodb2';
}
}
\ No newline at end of file
Examples
--------
**Retrieving All Models**
$users = User::all();
**Retrieving A Record By Primary Key**
$user = User::find('517c43667db388101e00000f');
**Wheres**
$users = User::where('votes', '>', 100)->take(10)->get();
**Or Statements**
$users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
**Using Where In With An Array**
$users = User::whereIn('id', array(1, 2, 3))->get();
**Order By**
$users = User::orderBy('name', 'desc')->get();
**Advanced Wheres**
$users = User::where('name', '=', 'John')->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
All basis insert, update, delete and select methods should be implemented. Feel free to fork and help completing this library!
\ No newline at end of file
src/Jenssegers/Mongodb/Query.php
View file @
3a6ad41b
...
@@ -215,19 +215,10 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -215,19 +215,10 @@ class Query extends \Illuminate\Database\Query\Builder {
$wheres
=
array
();
$wheres
=
array
();
foreach
(
$this
->
wheres
as
$i
=>
$where
)
foreach
(
$this
->
wheres
as
$i
=>
&
$where
)
{
{
// Nested query
if
(
$where
[
'type'
]
==
'Nested'
)
{
// Compile nested query and add it to current query
$compiled
=
$where
[
'query'
]
->
compileWheres
();
$wheres
=
array_merge_recursive
(
$wheres
,
$compiled
);
continue
;
}
// Convert id's
// Convert id's
if
(
$where
[
'column'
]
==
'_id'
)
if
(
isset
(
$where
[
'column'
])
&&
$where
[
'column'
]
==
'_id'
)
{
{
$where
[
'value'
]
=
(
$where
[
'value'
]
instanceof
MongoID
)
?
$where
[
'value'
]
:
new
MongoID
(
$where
[
'value'
]);
$where
[
'value'
]
=
(
$where
[
'value'
]
instanceof
MongoID
)
?
$where
[
'value'
]
:
new
MongoID
(
$where
[
'value'
]);
}
}
...
@@ -271,6 +262,21 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -271,6 +262,21 @@ class Query extends \Illuminate\Database\Query\Builder {
return
$query
;
return
$query
;
}
}
public
function
compileWhereNested
(
$where
)
{
extract
(
$where
);
// Compile subquery
$compiled
=
$query
->
compileWheres
();
if
(
$boolean
==
'or'
)
{
return
array
(
$this
->
conversion
[
$boolean
]
=>
array
(
$compiled
));
}
return
$compiled
;
}
public
function
compileWhereIn
(
$where
)
public
function
compileWhereIn
(
$where
)
{
{
extract
(
$where
);
extract
(
$where
);
...
@@ -280,16 +286,18 @@ class Query extends \Illuminate\Database\Query\Builder {
...
@@ -280,16 +286,18 @@ class Query extends \Illuminate\Database\Query\Builder {
public
function
compileWhereNull
(
$where
)
public
function
compileWhereNull
(
$where
)
{
{
extract
(
$where
);
$where
[
'operator'
]
=
'='
;
$where
[
'value'
]
=
null
;
return
array
(
$column
=>
array
(
$this
->
conversion
[
'exists'
]
=>
false
)
);
return
$this
->
compileWhereBasic
(
$where
);
}
}
public
function
compileWhereNotNull
(
$where
)
public
function
compileWhereNotNull
(
$where
)
{
{
extract
(
$where
);
$where
[
'operator'
]
=
'!='
;
$where
[
'value'
]
=
null
;
return
array
(
$column
=>
array
(
$this
->
conversion
[
'exists'
]
=>
true
)
);
return
$this
->
compileWhereBasic
(
$where
);
}
}
/**
/**
...
...
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