Skip to content

Commit 5f74a9b

Browse files
committed
bugfix mssql fetch
1 parent cf0b96d commit 5f74a9b

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

api.php

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Tqdev/PhpCrudApi/Record/OrderingInfo.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ public function getColumnOrdering(ReflectedTable $table, array $params): array
2626
}
2727
}
2828
if (count($fields) == 0) {
29-
$pk = $table->getPk();
30-
if ($pk) {
31-
$fields[] = [$pk->getName(), 'ASC'];
32-
} else {
33-
foreach ($table->getColumnNames() as $columnName) {
34-
$fields[] = [$columnName, 'ASC'];
35-
}
29+
return $this->getDefaultColumnOrdering($table);
30+
}
31+
return $fields;
32+
}
3633

34+
public function getDefaultColumnOrdering(ReflectedTable $table): array
35+
{
36+
$fields = array();
37+
$pk = $table->getPk();
38+
if ($pk) {
39+
$fields[] = [$pk->getName(), 'ASC'];
40+
} else {
41+
foreach ($table->getColumnNames() as $columnName) {
42+
$fields[] = [$columnName, 'ASC'];
3743
}
3844
}
3945
return $fields;

src/Tqdev/PhpCrudApi/Record/RelationJoiner.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class RelationJoiner
1717
public function __construct(ReflectionService $reflection, ColumnIncluder $columns)
1818
{
1919
$this->reflection = $reflection;
20+
$this->ordering = new OrderingInfo();
2021
$this->columns = $columns;
2122
}
2223

@@ -209,8 +210,12 @@ private function addPkRecords(ReflectedTable $t1, ReflectedTable $t2, array $pkV
209210
$conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
210211
}
211212
$condition = OrCondition::fromArray($conditions);
213+
$columnOrdering = array();
212214
$limit = VariableStore::get("joinLimits.maxRecords") ?: -1;
213-
foreach ($db->selectAll($t2, $columnNames, $condition, array(), 0, $limit) as $record) {
215+
if ($limit != -1) {
216+
$columnOrdering = $this->ordering->getDefaultColumnOrdering($t2);
217+
}
218+
foreach ($db->selectAll($t2, $columnNames, $condition, $columnOrdering, 0, $limit) as $record) {
214219
$records[] = $record;
215220
}
216221
}
@@ -255,9 +260,13 @@ private function getHabtmEmptyValues(ReflectedTable $t1, ReflectedTable $t2, Ref
255260

256261
$pkIds = implode(',', array_keys($pkValues));
257262
$condition = new ColumnCondition($t3->getColumn($fk1Name), 'in', $pkIds);
263+
$columnOrdering = array();
258264

259265
$limit = VariableStore::get("joinLimits.maxRecords") ?: -1;
260-
$records = $db->selectAll($t3, $columnNames, $condition, array(), 0, $limit);
266+
if ($limit != -1) {
267+
$columnOrdering = $this->ordering->getDefaultColumnOrdering($t3);
268+
}
269+
$records = $db->selectAll($t3, $columnNames, $condition, $columnOrdering, 0, $limit);
261270
foreach ($records as $record) {
262271
$val1 = $record[$fk1Name];
263272
$val2 = $record[$fk2Name];

0 commit comments

Comments
 (0)