Skip to content

Commit a181bb7

Browse files
committed
better fix for #513
1 parent ab148e2 commit a181bb7

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

api.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4917,10 +4917,13 @@ private function addJoinsForTables(ReflectedTable $t1, PathTree $joins, array &$
49174917
foreach ($joins->getKeys() as $t2Name) {
49184918

49194919
$t2 = $this->reflection->getTable($t2Name);
4920+
$t3 = null;
49204921

49214922
$belongsTo = count($t1->getFksTo($t2->getName())) > 0;
49224923
$hasMany = count($t2->getFksTo($t1->getName())) > 0;
4923-
$t3 = $this->hasAndBelongsToMany($t1, $t2);
4924+
if (!$belongsTo && !$hasMany) {
4925+
$t3 = $this->hasAndBelongsToMany($t1, $t2);
4926+
}
49244927
$hasAndBelongsToMany = ($t3 != null);
49254928

49264929
$newRecords = array();
@@ -4931,10 +4934,12 @@ private function addJoinsForTables(ReflectedTable $t1, PathTree $joins, array &$
49314934
if ($belongsTo) {
49324935
$fkValues = $this->getFkEmptyValues($t1, $t2, $records);
49334936
$this->addFkRecords($t2, $fkValues, $params, $db, $newRecords);
4934-
} elseif ($hasMany) {
4937+
}
4938+
if ($hasMany) {
49354939
$pkValues = $this->getPkEmptyValues($t1, $records);
49364940
$this->addPkRecords($t1, $t2, $pkValues, $params, $db, $newRecords);
4937-
} elseif ($hasAndBelongsToMany) {
4941+
}
4942+
if ($hasAndBelongsToMany) {
49384943
$habtmValues = $this->getHabtmEmptyValues($t1, $t2, $t3, $db, $records);
49394944
$this->addFkRecords($t2, $habtmValues->fkValues, $params, $db, $newRecords);
49404945
}
@@ -4944,10 +4949,12 @@ private function addJoinsForTables(ReflectedTable $t1, PathTree $joins, array &$
49444949
if ($fkValues != null) {
49454950
$this->fillFkValues($t2, $newRecords, $fkValues);
49464951
$this->setFkValues($t1, $t2, $records, $fkValues);
4947-
} elseif ($pkValues != null) {
4952+
}
4953+
if ($pkValues != null) {
49484954
$this->fillPkValues($t1, $t2, $newRecords, $pkValues);
49494955
$this->setPkValues($t1, $t2, $records, $pkValues);
4950-
} elseif ($habtmValues != null) {
4956+
}
4957+
if ($habtmValues != null) {
49514958
$this->fillFkValues($t2, $newRecords, $habtmValues->fkValues);
49524959
$this->setHabtmValues($t1, $t3, $records, $habtmValues);
49534960
}

src/Tqdev/PhpCrudApi/Record/RelationJoiner.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ private function addJoinsForTables(ReflectedTable $t1, PathTree $joins, array &$
9393
foreach ($joins->getKeys() as $t2Name) {
9494

9595
$t2 = $this->reflection->getTable($t2Name);
96-
96+
9797
$belongsTo = count($t1->getFksTo($t2->getName())) > 0;
9898
$hasMany = count($t2->getFksTo($t1->getName())) > 0;
99-
$t3 = $this->hasAndBelongsToMany($t1, $t2);
99+
if (!$belongsTo && !$hasMany) {
100+
$t3 = $this->hasAndBelongsToMany($t1, $t2);
101+
} else {
102+
$t3 = null;
103+
}
100104
$hasAndBelongsToMany = ($t3 != null);
101105

102106
$newRecords = array();
@@ -107,10 +111,12 @@ private function addJoinsForTables(ReflectedTable $t1, PathTree $joins, array &$
107111
if ($belongsTo) {
108112
$fkValues = $this->getFkEmptyValues($t1, $t2, $records);
109113
$this->addFkRecords($t2, $fkValues, $params, $db, $newRecords);
110-
} elseif ($hasMany) {
114+
}
115+
if ($hasMany) {
111116
$pkValues = $this->getPkEmptyValues($t1, $records);
112117
$this->addPkRecords($t1, $t2, $pkValues, $params, $db, $newRecords);
113-
} elseif ($hasAndBelongsToMany) {
118+
}
119+
if ($hasAndBelongsToMany) {
114120
$habtmValues = $this->getHabtmEmptyValues($t1, $t2, $t3, $db, $records);
115121
$this->addFkRecords($t2, $habtmValues->fkValues, $params, $db, $newRecords);
116122
}
@@ -120,10 +126,12 @@ private function addJoinsForTables(ReflectedTable $t1, PathTree $joins, array &$
120126
if ($fkValues != null) {
121127
$this->fillFkValues($t2, $newRecords, $fkValues);
122128
$this->setFkValues($t1, $t2, $records, $fkValues);
123-
} elseif ($pkValues != null) {
129+
}
130+
if ($pkValues != null) {
124131
$this->fillPkValues($t1, $t2, $newRecords, $pkValues);
125132
$this->setPkValues($t1, $t2, $records, $pkValues);
126-
} elseif ($habtmValues != null) {
133+
}
134+
if ($habtmValues != null) {
127135
$this->fillFkValues($t2, $newRecords, $habtmValues->fkValues);
128136
$this->setHabtmValues($t1, $t3, $records, $habtmValues);
129137
}

0 commit comments

Comments
 (0)