@@ -213,7 +213,7 @@ public function fields(array $fields=array(), $exclude=false) {
213213 } else
214214 $ this ->whitelist =$ fields ;
215215 $ id =$ this ->dbsType =='sql ' ?$ this ->primary :'_id ' ;
216- if (!in_array ($ id ,$ this ->whitelist ))
216+ if (!in_array ($ id ,$ this ->whitelist ) && !( $ exclude && in_array ( $ id , $ fields )) )
217217 $ this ->whitelist []=$ id ;
218218 $ this ->applyWhitelist ();
219219 return $ this ->whitelist ;
@@ -595,7 +595,7 @@ public function afind($filter = NULL, array $options = NULL, $ttl = 0, $rel_dept
595595 * @param array|null $filter
596596 * @param array|null $options
597597 * @param int $ttl
598- * @return CortexCollection
598+ * @return CortexCollection|false
599599 */
600600 public function find ($ filter = NULL , array $ options = NULL , $ ttl = 0 ) {
601601 $ sort =false ;
@@ -731,12 +731,12 @@ protected function filteredFind($filter = NULL, array $options = NULL, $ttl = 0,
731731 $ addToFilter = array ($ id .' IN ? ' , $ result );
732732 }
733733 // *-to-one
734- elseif ($ this ->dbsType == 'sql ' ) {
734+ elseif (! $ deep && $ this ->dbsType == 'sql ' ) {
735735 // use sub-query inclusion
736736 $ has_filter =$ this ->mergeFilter ([$ has_filter ,
737737 [$ this ->rel ($ key )->getTable ().'. ' .$ fromConf [1 ].'= ' .$ this ->getTable ().'. ' .$ id ]]);
738738 $ result = $ this ->_refSubQuery ($ key ,$ has_filter ,$ has_options );
739- $ addToFilter = ['exists( ' .$ result [0 ].') ' ]+ $ result [1 ];
739+ $ addToFilter = array_merge ( ['exists( ' .$ result [0 ].') ' ], $ result [1 ]) ;
740740 }
741741 elseif ($ result = $ this ->_hasRefsIn ($ key ,$ has_filter ,$ has_options ,$ ttl ))
742742 $ addToFilter = array ($ id .' IN ? ' , $ result );
@@ -781,11 +781,11 @@ protected function filteredFind($filter = NULL, array $options = NULL, $ttl = 0,
781781 $ options ['order ' ] = preg_replace ('/\h+DESC(?=\s*(?:$|,))/i ' ,' DESC NULLS LAST ' ,$ options ['order ' ]);
782782 // assemble full sql query for joined queries
783783 if ($ hasJoin ) {
784+ $ adhoc =[];
784785 // when in count-mode and grouping is active, wrap the query later
785786 // otherwise add a an adhoc counter field here
786787 if (!($ subquery_mode =($ options && !empty ($ options ['group ' ]))) && $ count )
787- $ this ->adhoc ['_rows ' ]=['expr ' =>'COUNT(*) ' ,'value ' =>NULL ];
788- $ adhoc =[];
788+ $ adhoc []='(COUNT(*)) as _rows ' ;
789789 if (!$ count )
790790 // add bind parameters for filters in adhoc fields
791791 if ($ this ->preBinds ) {
@@ -1224,7 +1224,7 @@ function save() {
12241224 // m:m save cascade
12251225 if (!empty ($ this ->saveCsd )) {
12261226 foreach ($ this ->saveCsd as $ key => $ val ) {
1227- if ($ fields [$ key ]['relType ' ] == 'has-many ' ) {
1227+ if ($ fields [$ key ]['relType ' ] == 'has-many ' ) {
12281228 $ relConf = $ fields [$ key ]['has-many ' ];
12291229 if ($ relConf ['hasRel ' ] == 'has-many ' ) {
12301230 $ mmTable = $ this ->mmTable ($ relConf ,$ key );
@@ -1236,12 +1236,12 @@ function save() {
12361236 $ filter [] = $ id ;
12371237 }
12381238 // delete all refs
1239- if (is_null ($ val ))
1239+ if (empty ($ val ))
12401240 $ mm ->erase ($ filter );
12411241 // update refs
12421242 elseif (is_array ($ val )) {
12431243 $ mm ->erase ($ filter );
1244- foreach ($ val as $ v ) {
1244+ foreach (array_unique ( $ val) as $ v ) {
12451245 if ($ relConf ['isSelf ' ] && $ v ==$ id )
12461246 continue ;
12471247 $ mm ->set ($ key ,$ v );
@@ -1256,7 +1256,7 @@ function save() {
12561256 $ rel = $ this ->getRelInstance ($ relConf [0 ],$ relConf ,$ key );
12571257 // find existing relations
12581258 $ refs = $ rel ->find ([$ relConf [1 ].' = ? ' ,$ this ->getRaw ($ relConf ['relField ' ])]);
1259- if (is_null ($ val )) {
1259+ if (empty ($ val )) {
12601260 foreach ($ refs ?:[] as $ model ) {
12611261 $ model ->set ($ relConf [1 ],NULL );
12621262 $ model ->save ();
@@ -1470,7 +1470,7 @@ function set($key, $val) {
14701470 // handle relations
14711471 if (isset ($ fields [$ key ]['belongs-to-one ' ])) {
14721472 // one-to-many, one-to-one
1473- if (is_null ($ val ))
1473+ if (empty ($ val ))
14741474 $ val = NULL ;
14751475 elseif (is_object ($ val ) &&
14761476 !($ this ->dbsType =='mongo ' && (
@@ -1489,7 +1489,7 @@ function set($key, $val) {
14891489 $ val = $ this ->db ->legacy () ? new \MongoId ($ val ) : new \MongoDB \BSON \ObjectId ($ val );
14901490 } elseif (isset ($ fields [$ key ]['has-one ' ])){
14911491 $ relConf = $ fields [$ key ]['has-one ' ];
1492- if (is_null ($ val )) {
1492+ if (empty ($ val )) {
14931493 $ val = $ this ->get ($ key );
14941494 $ val ->set ($ relConf [1 ],NULL );
14951495 } else {
@@ -2554,7 +2554,7 @@ public function sql_quoteCondition($cond, $db) {
25542554 function ($ match ) use ($ db ) {
25552555 if (!isset ($ match [1 ]))
25562556 return $ match [0 ];
2557- if (preg_match ('/\b(AND|OR|IN|LIKE|NOT)\b/i ' ,$ match [1 ]))
2557+ if (preg_match ('/\b(AND|OR|IN|LIKE|NOT|HAVING|SELECT|FROM|WHERE )\b/i ' ,$ match [1 ]))
25582558 return $ match [1 ];
25592559 return $ db ->quotekey ($ match [1 ]);
25602560 }, $ cond );
@@ -2576,7 +2576,7 @@ public function sql_prependTableToFields($cond, $table) {
25762576 function ($ match ) use ($ table ) {
25772577 if (!isset ($ match [3 ]))
25782578 return $ match [1 ];
2579- if (preg_match ('/\b(AND|OR|IN|LIKE|NOT)\b/i ' ,$ match [3 ]))
2579+ if (preg_match ('/\b(AND|OR|IN|LIKE|NOT|HAVING|SELECT|FROM|WHERE )\b/i ' ,$ match [3 ]))
25802580 return $ match [0 ];
25812581 return $ match [2 ].$ table .'. ' .$ match [3 ];
25822582 }, $ cond );
0 commit comments