Commit 43230579 authored by Jens Segers's avatar Jens Segers

Added like regexp

parent d50a4b4b
...@@ -91,6 +91,8 @@ Examples ...@@ -91,6 +91,8 @@ Examples
**Group By** **Group By**
Selected columns that are not grouped will be aggregated with the $last function.
$users = Users::groupBy('title')->get(array('title', 'name')); $users = Users::groupBy('title')->get(array('title', 'name'));
**Aggregation** **Aggregation**
...@@ -99,5 +101,10 @@ Examples ...@@ -99,5 +101,10 @@ Examples
$price = Order::min('price'); $price = Order::min('price');
$price = Order::avg('price'); $price = Order::avg('price');
$total = User::sum('votes'); $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! 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; <?php namespace Jenssegers\Mongodb;
use MongoID; use MongoID;
use MongoRegex;
class Query extends \Illuminate\Database\Query\Builder { class Query extends \Illuminate\Database\Query\Builder {
...@@ -79,7 +80,7 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -79,7 +80,7 @@ class Query extends \Illuminate\Database\Query\Builder {
{ {
$pipeline = array(); $pipeline = array();
// Group // Grouping
$group = array(); $group = array();
$group['_id'] = $this->groups ? $this->groups : 0; $group['_id'] = $this->groups ? $this->groups : 0;
...@@ -92,9 +93,17 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -92,9 +93,17 @@ class Query extends \Illuminate\Database\Query\Builder {
// Apply aggregation functions // Apply aggregation functions
if ($this->aggregate) if ($this->aggregate)
{ {
$function = $this->aggregate['function'];
foreach ($this->aggregate['columns'] as $column) 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 { ...@@ -150,7 +159,7 @@ class Query extends \Illuminate\Database\Query\Builder {
* *
* @param string $column * @param string $column
* @param string $direction * @param string $direction
* @return \Illuminate\Database\Query\Builder * @return Builder
*/ */
public function orderBy($column, $direction = 'asc') public function orderBy($column, $direction = 'asc')
{ {
...@@ -163,7 +172,7 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -163,7 +172,7 @@ class Query extends \Illuminate\Database\Query\Builder {
* Add a "group by" clause to the query. * Add a "group by" clause to the query.
* *
* @param dynamic $columns * @param dynamic $columns
* @return \Illuminate\Database\Query\Builder * @return Builder
*/ */
public function groupBy() public function groupBy()
{ {
...@@ -312,6 +321,19 @@ class Query extends \Illuminate\Database\Query\Builder { ...@@ -312,6 +321,19 @@ class Query extends \Illuminate\Database\Query\Builder {
{ {
extract($where); 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 == '=') if (!isset($operator) || $operator == '=')
{ {
$query = array($column => $value); $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