@@ -443,39 +443,72 @@ public function find($criteria=null)
443
443
elseif (!$ this ->_joined && !empty ($ this ->_parent ->records )) // not joined before
444
444
{
445
445
if (empty ($ this ->relation ->through ) && count ((array )$ this ->relation ->foreignKey ) === count ((array )$ this ->_parent ->_pkAlias )) {
446
- $ query = new CJoinQuery ($ this );
447
- $ this ->_joined = true ;
448
- $ query ->selects = array (); // reset to not receive the extra keys
449
- $ query ->selects [] = $ this ->getColumnSelect ($ this ->relation ->select );
450
- $ query ->selects [] = $ this ->getRelationsKeys ();
451
- $ this ->buildQuery ($ query );
452
- $ query ->conditions [] = $ this ->relation ->on ;
453
- $ query ->conditions [] = $ this ->buildConditions ();
454
- $ query ->orders [] = $ this ->relation ->order ;
446
+ if ($ this ->model ->tableSpaceKey ()) {
447
+ $ this ->queryWithGroupKey ($ this ->model ->tableSpaceKey ());
448
+ } else {
449
+ $ query = new CJoinQuery ($ this );
450
+ $ this ->_joined = true ;
451
+ $ query ->selects = []; // reset to not receive the extra keys
452
+ $ query ->selects [] = $ this ->getColumnSelect ($ this ->relation ->select );
453
+ $ query ->selects [] = $ this ->getRelationsKeys ();
454
+ $ this ->buildQuery ($ query );
455
+ $ query ->conditions [] = $ this ->relation ->on ;
456
+ $ query ->conditions [] = $ this ->buildConditions ();
457
+ $ query ->orders [] = $ this ->relation ->order ;
458
+ $ this ->_parent ->runQuery ($ query );
459
+ }
455
460
} else {
456
461
$ query =new CJoinQuery ($ this ->_parent );
457
462
$ this ->_joined =true ;
458
463
$ query ->join ($ this );
459
464
$ this ->buildQuery ($ query );
460
- }
461
- $ this -> _parent -> runQuery ( $ query );
465
+ $ this -> _parent -> runQuery ( $ query );
466
+ }
462
467
}
463
468
464
- foreach ($ this ->children as $ child ) // find recursively
465
- $ child ->find ();
469
+ foreach ($ this ->children as $ child ) // find recursively
470
+ $ child ->find ();
466
471
467
- foreach ($ this ->stats as $ stat )
468
- $ stat ->query ();
469
- }
472
+ foreach ($ this ->stats as $ stat )
473
+ $ stat ->query ();
474
+ }
475
+
476
+ private function queryWithGroupKey ($ groupKey )
477
+ {
478
+ if (is_array ($ this ->_parent ->_table ->primaryKey )) {
479
+ $ values = array_keys ($ this ->_parent ->records );
480
+ $ groupingValues = [];
481
+ foreach ($ values as $ value ) {
482
+ $ value = unserialize ($ value );
483
+ $ groupingValues [$ value [$ groupKey ]][] = $ value ;
484
+ }
485
+ }
486
+
487
+ $ query = new CJoinQuery ($ this );
488
+ $ this ->_joined = true ;
489
+ $ query ->selects = []; // reset to not receive the extra keys
490
+ $ query ->selects [] = $ this ->getColumnSelect ($ this ->relation ->select );
491
+ $ query ->selects [] = $ this ->getRelationsKeys ();
492
+ $ this ->buildQuery ($ query );
493
+ $ query ->orders [] = $ this ->relation ->order ;
494
+
495
+ foreach ($ groupingValues as $ groupingValue ) {
496
+ $ query ->conditions = [];
497
+ $ query ->conditions [] = $ this ->relation ->on ;
498
+ $ query ->conditions [] = $ this ->buildConditions ($ groupingValue );
499
+ $ this ->_parent ->runQuery ($ query );
500
+ }
501
+ }
470
502
471
- public function buildConditions ()
503
+ private function buildConditions ($ values = null )
472
504
{
473
- $ values =array_keys ($ this ->_parent ->records );
474
- if (is_array ($ this ->_parent ->_table ->primaryKey ))
475
- {
476
- foreach ($ values as &$ value )
477
- $ value =unserialize ($ value );
478
- unset($ value );
505
+ if (empty ($ values )) {
506
+ $ values = array_keys ($ this ->_parent ->records );
507
+ if (is_array ($ this ->_parent ->_table ->primaryKey )) {
508
+ foreach ($ values as &$ value )
509
+ $ value = unserialize ($ value );
510
+ unset($ value );
511
+ }
479
512
}
480
513
if (is_array ($ this ->_parent ->_table ->primaryKey )) {
481
514
$ keys = array ();
@@ -493,7 +526,7 @@ public function buildConditions()
493
526
return $ this ->_builder ->createInCondition ($ this ->_table ,$ keys ,$ values ,$ this ->getColumnPrefix ());
494
527
}
495
528
496
- public function getRelationsKeys ()
529
+ private function getRelationsKeys ()
497
530
{
498
531
$ fields = array ();
499
532
$ foreignKey = $ this ->relation ->foreignKey ;
0 commit comments