7
7
class Builder extends \Illuminate \Database \Query \Builder {
8
8
9
9
/**
10
- * The database collection
11
- *
12
- * @var MongoCollection
13
- */
10
+ * The database collection
11
+ *
12
+ * @var MongoCollection
13
+ */
14
14
protected $ collection ;
15
15
16
16
/**
17
- * All of the available operators.
18
- *
19
- * @var array
20
- */
17
+ * All of the available operators.
18
+ *
19
+ * @var array
20
+ */
21
21
protected $ conversion = array (
22
22
'= ' => '= ' ,
23
23
'!= ' => '$ne ' ,
@@ -29,11 +29,11 @@ class Builder extends \Illuminate\Database\Query\Builder {
29
29
);
30
30
31
31
/**
32
- * Create a new query builder instance.
33
- *
34
- * @param Connection $connection
35
- * @return void
36
- */
32
+ * Create a new query builder instance.
33
+ *
34
+ * @param Connection $connection
35
+ * @return void
36
+ */
37
37
public function __construct (Connection $ connection )
38
38
{
39
39
$ this ->connection = $ connection ;
@@ -339,24 +339,15 @@ public function update(array $values, array $options = array())
339
339
*/
340
340
public function increment ($ column , $ amount = 1 , array $ extra = array ())
341
341
{
342
- // build update statement
343
- $ update = array (
342
+ $ query = array (
344
343
'$inc ' => array ($ column => $ amount ),
345
344
'$set ' => $ extra ,
346
345
);
347
346
348
- // protect
347
+ // Protect
349
348
$ this ->whereNotNull ($ column );
350
349
351
- // perform
352
- $ result = $ this ->collection ->update ($ this ->compileWheres (), $ update , array ('multiple ' => true ));
353
-
354
- if (1 == (int ) $ result ['ok ' ])
355
- {
356
- return $ result ['n ' ];
357
- }
358
-
359
- return 0 ;
350
+ return $ this ->performUpdate ($ query );
360
351
}
361
352
362
353
/**
@@ -505,6 +496,28 @@ public function pull($column, $value = null)
505
496
return $ this ->performUpdate ($ query );
506
497
}
507
498
499
+ /**
500
+ * Remove one or more fields.
501
+ *
502
+ * @param mixed $columns
503
+ * @return int
504
+ */
505
+ public function dropColumn ($ columns )
506
+ {
507
+ if (!is_array ($ columns )) $ columns = array ($ columns );
508
+
509
+ $ fields = array ();
510
+
511
+ foreach ($ columns as $ column )
512
+ {
513
+ $ fields [$ column ] = 1 ;
514
+ }
515
+
516
+ $ query = array ('$unset ' => $ fields );
517
+
518
+ return $ this ->performUpdate ($ query );
519
+ }
520
+
508
521
/**
509
522
* Get a new instance of the query builder.
510
523
*
@@ -516,7 +529,7 @@ public function newQuery()
516
529
}
517
530
518
531
/**
519
- * Perform update.
532
+ * Perform an update query .
520
533
*
521
534
* @param array $query
522
535
* @param array $options
@@ -541,7 +554,7 @@ protected function performUpdate($query, array $options = array())
541
554
}
542
555
543
556
/**
544
- * Convert a key to MongoID if needed
557
+ * Convert a key to MongoID if needed.
545
558
*
546
559
* @param mixed $id
547
560
* @return mixed
@@ -557,10 +570,10 @@ protected function convertKey($id)
557
570
}
558
571
559
572
/**
560
- * Compile the where array
561
- *
562
- * @return array
563
- */
573
+ * Compile the where array.
574
+ *
575
+ * @return array
576
+ */
564
577
protected function compileWheres ()
565
578
{
566
579
if (!$ this ->wheres ) return array ();
@@ -694,4 +707,21 @@ protected function compileWhereRaw($where)
694
707
return $ where ['sql ' ];
695
708
}
696
709
710
+ /**
711
+ * Handle dynamic method calls into the method.
712
+ *
713
+ * @param string $method
714
+ * @param array $parameters
715
+ * @return mixed
716
+ */
717
+ public function __call ($ method , $ parameters )
718
+ {
719
+ if ($ method == 'unset ' )
720
+ {
721
+ return call_user_func_array (array ($ this , 'dropColumn ' ), $ parameters );
722
+ }
723
+
724
+ return parent ::__call ($ method , $ parameters );
725
+ }
726
+
697
727
}
0 commit comments