@@ -3254,13 +3254,13 @@ public function handle(Request $request): Response
32543254 $ joinPath = array ();
32553255 $ tables = explode (', ' , $ params ['join ' ][$ i ]);
32563256 for ($ depth = 0 ; $ depth < min ($ maxDepth , count ($ tables )); $ depth ++) {
3257- array_push ($ joinPath , $ table );
3257+ array_push ($ joinPath , $ tables [ $ depth ] );
32583258 $ tableCount += 1 ;
32593259 if ($ tableCount == $ maxTables ) {
32603260 break ;
32613261 }
32623262 }
3263- array_push ($ joinPaths , $ joinPath );
3263+ array_push ($ joinPaths , implode ( ' , ' , $ joinPath) );
32643264 if ($ tableCount == $ maxTables ) {
32653265 break ;
32663266 }
@@ -4583,14 +4583,20 @@ public function getColumnOrdering(ReflectedTable $table, array $params): array
45834583 }
45844584 }
45854585 if (count ($ fields ) == 0 ) {
4586- $ pk = $ table ->getPk ();
4587- if ($ pk ) {
4588- $ fields [] = [$ pk ->getName (), 'ASC ' ];
4589- } else {
4590- foreach ($ table ->getColumnNames () as $ columnName ) {
4591- $ fields [] = [$ columnName , 'ASC ' ];
4592- }
4586+ return $ this ->getDefaultColumnOrdering ($ table );
4587+ }
4588+ return $ fields ;
4589+ }
45934590
4591+ public function getDefaultColumnOrdering (ReflectedTable $ table ): array
4592+ {
4593+ $ fields = array ();
4594+ $ pk = $ table ->getPk ();
4595+ if ($ pk ) {
4596+ $ fields [] = [$ pk ->getName (), 'ASC ' ];
4597+ } else {
4598+ foreach ($ table ->getColumnNames () as $ columnName ) {
4599+ $ fields [] = [$ columnName , 'ASC ' ];
45944600 }
45954601 }
45964602 return $ fields ;
@@ -4875,6 +4881,7 @@ class RelationJoiner
48754881 public function __construct (ReflectionService $ reflection , ColumnIncluder $ columns )
48764882 {
48774883 $ this ->reflection = $ reflection ;
4884+ $ this ->ordering = new OrderingInfo ();
48784885 $ this ->columns = $ columns ;
48794886 }
48804887
@@ -4992,7 +4999,7 @@ private function addJoinsForTables(ReflectedTable $t1, PathTree $joins, array &$
49924999 }
49935000 if ($ habtmValues != null ) {
49945001 $ this ->fillFkValues ($ t2 , $ newRecords , $ habtmValues ->fkValues );
4995- $ this ->setHabtmValues ($ t1 , $ t3 , $ records , $ habtmValues );
5002+ $ this ->setHabtmValues ($ t1 , $ t2 , $ records , $ habtmValues );
49965003 }
49975004 }
49985005 }
@@ -5067,8 +5074,12 @@ private function addPkRecords(ReflectedTable $t1, ReflectedTable $t2, array $pkV
50675074 $ conditions [] = new ColumnCondition ($ fk , 'in ' , $ pkValueKeys );
50685075 }
50695076 $ condition = OrCondition::fromArray ($ conditions );
5077+ $ columnOrdering = array ();
50705078 $ limit = VariableStore::get ("joinLimits.maxRecords " ) ?: -1 ;
5071- foreach ($ db ->selectAll ($ t2 , $ columnNames , $ condition , array (), 0 , $ limit ) as $ record ) {
5079+ if ($ limit != -1 ) {
5080+ $ columnOrdering = $ this ->ordering ->getDefaultColumnOrdering ($ t2 );
5081+ }
5082+ foreach ($ db ->selectAll ($ t2 , $ columnNames , $ condition , $ columnOrdering , 0 , $ limit ) as $ record ) {
50725083 $ records [] = $ record ;
50735084 }
50745085 }
@@ -5113,9 +5124,13 @@ private function getHabtmEmptyValues(ReflectedTable $t1, ReflectedTable $t2, Ref
51135124
51145125 $ pkIds = implode (', ' , array_keys ($ pkValues ));
51155126 $ condition = new ColumnCondition ($ t3 ->getColumn ($ fk1Name ), 'in ' , $ pkIds );
5127+ $ columnOrdering = array ();
51165128
51175129 $ limit = VariableStore::get ("joinLimits.maxRecords " ) ?: -1 ;
5118- $ records = $ db ->selectAll ($ t3 , $ columnNames , $ condition , array (), 0 , $ limit );
5130+ if ($ limit != -1 ) {
5131+ $ columnOrdering = $ this ->ordering ->getDefaultColumnOrdering ($ t3 );
5132+ }
5133+ $ records = $ db ->selectAll ($ t3 , $ columnNames , $ condition , $ columnOrdering , 0 , $ limit );
51195134 foreach ($ records as $ record ) {
51205135 $ val1 = $ record [$ fk1Name ];
51215136 $ val2 = $ record [$ fk2Name ];
@@ -5126,18 +5141,18 @@ private function getHabtmEmptyValues(ReflectedTable $t1, ReflectedTable $t2, Ref
51265141 return new HabtmValues ($ pkValues , $ fkValues );
51275142 }
51285143
5129- private function setHabtmValues (ReflectedTable $ t1 , ReflectedTable $ t3 , array &$ records , HabtmValues $ habtmValues ) /*: void*/
5144+ private function setHabtmValues (ReflectedTable $ t1 , ReflectedTable $ t2 , array &$ records , HabtmValues $ habtmValues ) /*: void*/
51305145 {
51315146 $ pkName = $ t1 ->getPk ()->getName ();
5132- $ t3Name = $ t3 ->getName ();
5147+ $ t2Name = $ t2 ->getName ();
51335148 foreach ($ records as $ i => $ record ) {
51345149 $ key = $ record [$ pkName ];
51355150 $ val = array ();
51365151 $ fks = $ habtmValues ->pkValues [$ key ];
51375152 foreach ($ fks as $ fk ) {
51385153 $ val [] = $ habtmValues ->fkValues [$ fk ];
51395154 }
5140- $ records [$ i ][$ t3Name ] = $ val ;
5155+ $ records [$ i ][$ t2Name ] = $ val ;
51415156 }
51425157 }
51435158}
0 commit comments