@@ -667,78 +667,88 @@ public function convertKey($id)
667
667
*/
668
668
protected function compileWheres ()
669
669
{
670
- if (!$ this ->wheres ) return array ();
670
+ // The where's to compile.
671
+ $ wheres = $ this ->wheres ?: array ();
671
672
672
- // The new list of compiled wheres
673
- $ wheres = array ();
673
+ // We will add all compiled wheres to this array.
674
+ $ compiled = array ();
674
675
675
- foreach ($ this -> wheres as $ i => &$ where )
676
+ foreach ($ wheres as $ i => &$ where )
676
677
{
677
- // Make sure the operator is in lowercase
678
+ // Make sure the operator is in lowercase.
678
679
if (isset ($ where ['operator ' ]))
679
680
{
680
681
$ where ['operator ' ] = strtolower ($ where ['operator ' ]);
681
682
682
- // Fix elemMatch
683
+ // Fix elemMatch.
683
684
if ($ where ['operator ' ] == 'elemmatch ' )
684
685
{
685
686
$ where ['operator ' ] = 'elemMatch ' ;
686
687
}
687
688
}
688
689
689
- // Convert id's
690
+ // Convert id's.
690
691
if (isset ($ where ['column ' ]) && $ where ['column ' ] == '_id ' )
691
692
{
692
- // Multiple values
693
+ // Multiple values.
693
694
if (isset ($ where ['values ' ]))
694
695
{
695
696
foreach ($ where ['values ' ] as &$ value )
696
697
{
697
698
$ value = $ this ->convertKey ($ value );
698
699
}
699
700
}
700
- // Single value
701
- elseif (isset ($ where ['value ' ]))
701
+
702
+ // Single value.
703
+ else if (isset ($ where ['value ' ]))
702
704
{
703
705
$ where ['value ' ] = $ this ->convertKey ($ where ['value ' ]);
704
706
}
705
707
}
706
708
707
- // Convert dates
709
+ // Convert DateTime values to MongoDate.
708
710
if (isset ($ where ['value ' ]) && $ where ['value ' ] instanceof DateTime)
709
711
{
710
712
$ where ['value ' ] = new MongoDate ($ where ['value ' ]->getTimestamp ());
711
713
}
712
714
713
- // First item of chain
714
- if ($ i == 0 && count ($ this ->wheres ) > 1 && $ where ['boolean ' ] == 'and ' )
715
+ // The next item in a "chain" of wheres devices the boolean of the
716
+ // first item. So if we see that there are multiple wheres, we will
717
+ // use the operator of the next where.
718
+ if ($ i == 0 and count ($ wheres ) > 1 and $ where ['boolean ' ] == 'and ' )
715
719
{
716
- // Copy over boolean value of next item in chain
717
- $ where ['boolean ' ] = $ this ->wheres [$ i +1 ]['boolean ' ];
720
+ $ where ['boolean ' ] = $ wheres [$ i +1 ]['boolean ' ];
718
721
}
719
722
720
- // Delegate
723
+ // We use different methods to compile different wheres.
721
724
$ method = "compileWhere {$ where ['type ' ]}" ;
722
- $ compiled = $ this ->{$ method }($ where );
725
+ $ result = $ this ->{$ method }($ where );
723
726
724
- // Check for or
727
+ // Wrap the where with an $or operator.
725
728
if ($ where ['boolean ' ] == 'or ' )
726
729
{
727
- $ compiled = array ('$or ' => array ($ compiled ));
730
+ $ result = array ('$or ' => array ($ result ));
731
+ }
732
+
733
+ // If there are multiple wheres, we will wrap it with $and. This is needed
734
+ // to make nested wheres work.
735
+ else if (count ($ wheres ) > 1 )
736
+ {
737
+ $ result = array ('$and ' => array ($ result ));
728
738
}
729
739
730
- // Merge compiled where
731
- $ wheres = array_merge_recursive ($ wheres , $ compiled );
740
+ // Merge the compiled where with the others.
741
+ $ compiled = array_merge_recursive ($ compiled , $ result );
732
742
}
733
743
734
- return $ wheres ;
744
+ return $ compiled ;
735
745
}
736
746
737
747
protected function compileWhereBasic ($ where )
738
748
{
739
749
extract ($ where );
740
750
741
- // Replace like with MongoRegex
751
+ // Replace like with a MongoRegex instance.
742
752
if ($ operator == 'like ' )
743
753
{
744
754
$ operator = '= ' ;
@@ -751,7 +761,7 @@ protected function compileWhereBasic($where)
751
761
$ value = new MongoRegex ("/ $ regex/i " );
752
762
}
753
763
754
- if (! isset ($ operator ) || $ operator == '= ' )
764
+ if ( ! isset ($ operator ) or $ operator == '= ' )
755
765
{
756
766
$ query = array ($ column => $ value );
757
767
}
0 commit comments