Skip to content

Commit 7dbbb2b

Browse files
committed
Fix many-to-many relationship bug
- Version table is now selected when using a many-to-many relationship
1 parent 29916b4 commit 7dbbb2b

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/BuilderTrait.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@
66

77
trait BuilderTrait
88
{
9+
/**
10+
* Get the hydrated models without eager loading.
11+
*
12+
* @param array $columns
13+
* @return \Illuminate\Database\Eloquent\Model[]
14+
*/
15+
public function getModels($columns = array('*'))
16+
{
17+
// make sure that we select the version table, if the main table is selected
18+
$tempColumns = isset($this->query->columns)
19+
? array_merge($columns, $this->query->columns)
20+
: $columns;
21+
foreach($tempColumns as $column) {
22+
$segments = explode('.', $column);
23+
if ($segments[0] == $this->model->getTable()) {
24+
$this->query->addSelect($this->model->getVersionTable().'.*');
25+
break;
26+
}
27+
}
28+
29+
return parent::getModels($columns);
30+
}
31+
932
/**
1033
* Insert a new record into the database.
1134
*
@@ -227,18 +250,18 @@ protected function getVersionValues(array $values)
227250
*/
228251
protected function isVersionedKey($key, array $versionedKeys)
229252
{
230-
$keyFractions = explode(".",$key);
253+
$segments = explode(".",$key);
231254

232-
if (count($keyFractions) > 2) {
255+
if (count($segments) > 2) {
233256
throw new Exception("Key '".$key."' has too many fractions.");
234257
}
235258

236-
if (count($keyFractions) == 1 && in_array($keyFractions[0], $versionedKeys)) {
237-
return $keyFractions[0];
259+
if (count($segments) == 1 && in_array($segments[0], $versionedKeys)) {
260+
return $segments[0];
238261
}
239262

240-
if (count($keyFractions) == 2 && $keyFractions[0] == $this->model->getVersionTable() && in_array($keyFractions[1], $versionedKeys)) {
241-
return $keyFractions[1];
263+
if (count($segments) == 2 && $segments[0] == $this->model->getVersionTable() && in_array($segments[1], $versionedKeys)) {
264+
return $segments[1];
242265
}
243266

244267
return null;

0 commit comments

Comments
 (0)