Commit 3a6ad41b authored by Jens Segers's avatar Jens Segers

Advanced queries and readme update

parent f8054f1c
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
...@@ -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);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment