Commit 43230579 authored by Jens Segers's avatar Jens Segers

Added like regexp

parent d50a4b4b
......@@ -91,6 +91,8 @@ Examples
**Group By**
Selected columns that are not grouped will be aggregated with the $last function.
$users = Users::groupBy('title')->get(array('title', 'name'));
**Aggregation**
......@@ -99,5 +101,10 @@ Examples
$price = Order::min('price');
$price = Order::avg('price');
$total = User::sum('votes');
$total = Order::count();
**Like**
$user = Comment::where('body', 'like', '%spam%')->get();
All basic insert, update, delete and select methods should be implemented. Feel free to fork and help completing this library!
\ No newline at end of file
<?php namespace Jenssegers\Mongodb;
use MongoID;
use MongoRegex;
class Query extends \Illuminate\Database\Query\Builder {
......@@ -79,7 +80,7 @@ class Query extends \Illuminate\Database\Query\Builder {
{
$pipeline = array();
// Group
// Grouping
$group = array();
$group['_id'] = $this->groups ? $this->groups : 0;
......@@ -92,9 +93,17 @@ class Query extends \Illuminate\Database\Query\Builder {
// Apply aggregation functions
if ($this->aggregate)
{
$function = $this->aggregate['function'];
foreach ($this->aggregate['columns'] as $column)
{
$group[$column] = array('$' . $this->aggregate['function'] => '$' . $column);
if ($function == 'count')
{
$group[$column] = array('$sum' => 1);
}
else {
$group[$column] = array('$' . $function => '$' . $column);
}
}
}
......@@ -150,7 +159,7 @@ class Query extends \Illuminate\Database\Query\Builder {
*
* @param string $column
* @param string $direction
* @return \Illuminate\Database\Query\Builder
* @return Builder
*/
public function orderBy($column, $direction = 'asc')
{
......@@ -163,7 +172,7 @@ class Query extends \Illuminate\Database\Query\Builder {
* Add a "group by" clause to the query.
*
* @param dynamic $columns
* @return \Illuminate\Database\Query\Builder
* @return Builder
*/
public function groupBy()
{
......@@ -312,6 +321,19 @@ class Query extends \Illuminate\Database\Query\Builder {
{
extract($where);
// Replace like with MongoRegex
if ($operator == 'like')
{
$operator = '=';
$regex = str_replace('%', '', $value);
// Prepare regex
if (substr($value, 0, 1) != '%') $regex = '^' . $regex;
if (substr($value, -1) != '%') $regex = $regex . '$';
$value = new MongoRegex("/$regex/i");
}
if (!isset($operator) || $operator == '=')
{
$query = array($column => $value);
......
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