Skip to content

Commit 924837f

Browse files
committed
Making progress on fixing relations
1 parent 3e26e05 commit 924837f

File tree

5 files changed

+100
-25
lines changed

5 files changed

+100
-25
lines changed

src/Jenssegers/Mongodb/Relations/BelongsTo.php

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Jenssegers\Mongodb\Relations;
22

3+
use Illuminate\Database\Eloquent\Builder;
4+
35
class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo
46
{
57
/**
@@ -28,6 +30,14 @@ public function addEagerConstraints(array $models)
2830
$this->query->whereIn($key, $this->getEagerModelKeys($models));
2931
}
3032

33+
/**
34+
* @inheritdoc
35+
*/
36+
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
37+
{
38+
return $query;
39+
}
40+
3141
/**
3242
* Get the owner key with backwards compatible support.
3343
*/

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

+35
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
<?php namespace Jenssegers\Mongodb\Relations;
22

3+
use Illuminate\Database\Eloquent\Builder;
34
use Illuminate\Database\Eloquent\Collection;
45
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
67

78
class BelongsToMany extends EloquentBelongsToMany
89
{
10+
/**
11+
* Get the key for comparing against the parent key in "has" query.
12+
*
13+
* @return string
14+
*/
15+
public function getHasCompareKey()
16+
{
17+
return $this->getForeignKey();
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
24+
{
25+
return $query;
26+
}
27+
928
/**
1029
* @inheritdoc
1130
*/
@@ -25,6 +44,14 @@ protected function getSelectColumns(array $columns = ['*'])
2544
return $columns;
2645
}
2746

47+
/**
48+
* @inheritdoc
49+
*/
50+
protected function shouldSelect(array $columns = ['*'])
51+
{
52+
return $columns;
53+
}
54+
2855
/**
2956
* @inheritdoc
3057
*/
@@ -260,6 +287,14 @@ public function getForeignKey()
260287
return $this->foreignKey;
261288
}
262289

290+
/**
291+
* @inheritdoc
292+
*/
293+
public function getQualifiedForeignKeyName()
294+
{
295+
return $this->foreignKey;
296+
}
297+
263298
/**
264299
* Format the sync list so that it is keyed by ID. (Legacy Support)
265300
* The original function has been renamed to formatRecordsList since Laravel 5.3

src/Jenssegers/Mongodb/Relations/HasMany.php

+21-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55

66
class HasMany extends EloquentHasMany
77
{
8+
/**
9+
* Get the key for comparing against the parent key in "has" query.
10+
*
11+
* @return string
12+
*/
13+
public function getHasCompareKey()
14+
{
15+
return $this->getForeignKeyName();
16+
}
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
22+
{
23+
$foreignKey = $this->getHasCompareKey();
24+
25+
return $query->select($foreignKey)->where($foreignKey, 'exists', true);
26+
}
27+
828
/**
929
* Add the constraints for a relationship count query.
1030
*
@@ -16,7 +36,7 @@ public function getRelationCountQuery(Builder $query, Builder $parent)
1636
{
1737
$foreignKey = $this->getHasCompareKey();
1838

19-
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true);
39+
return $query->select($foreignKey)->where($foreignKey, 'exists', true);
2040
}
2141

2242
/**
@@ -35,14 +55,4 @@ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*
3555

3656
return $query->where($this->getHasCompareKey(), 'exists', true);
3757
}
38-
39-
/**
40-
* Get the plain foreign key.
41-
*
42-
* @return string
43-
*/
44-
public function getPlainForeignKey()
45-
{
46-
return $this->getForeignKey();
47-
}
4858
}

src/Jenssegers/Mongodb/Relations/HasOne.php

+33-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@
55

66
class HasOne extends EloquentHasOne
77
{
8+
/**
9+
* Get the key for comparing against the parent key in "has" query.
10+
*
11+
* @return string
12+
*/
13+
public function getForeignKeyName()
14+
{
15+
return $this->foreignKey;
16+
}
17+
18+
/**
19+
* Get the key for comparing against the parent key in "has" query.
20+
*
21+
* @return string
22+
*/
23+
public function getHasCompareKey()
24+
{
25+
return $this->getForeignKeyName();
26+
}
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
32+
{
33+
$foreignKey = $this->getForeignKeyName();
34+
35+
return $query->select($foreignKey)->where($foreignKey, 'exists', true);
36+
}
37+
838
/**
939
* Add the constraints for a relationship count query.
1040
*
@@ -14,9 +44,9 @@ class HasOne extends EloquentHasOne
1444
*/
1545
public function getRelationCountQuery(Builder $query, Builder $parent)
1646
{
17-
$foreignKey = $this->getHasCompareKey();
47+
$foreignKey = $this->getForeignKeyName();
1848

19-
return $query->select($this->getHasCompareKey())->where($this->getHasCompareKey(), 'exists', true);
49+
return $query->select($foreignKey)->where($foreignKey, 'exists', true);
2050
}
2151

2252
/**
@@ -33,16 +63,6 @@ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*
3363

3464
$key = $this->wrap($this->getQualifiedParentKeyName());
3565

36-
return $query->where($this->getHasCompareKey(), 'exists', true);
37-
}
38-
39-
/**
40-
* Get the plain foreign key.
41-
*
42-
* @return string
43-
*/
44-
public function getPlainForeignKey()
45-
{
46-
return $this->getForeignKey();
66+
return $query->where($this->getForeignKeyName(), 'exists', true);
4767
}
4868
}

src/Jenssegers/Mongodb/Relations/MorphTo.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function getResultsByType($type)
2828

2929
$query = $instance->newQuery();
3030

31-
return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
31+
return $query->whereIn($key, $this->gatherKeysByType($type))->get();
3232
}
3333

3434
/**

0 commit comments

Comments
 (0)