Skip to content

Commit 73b5406

Browse files
author
David Nahodyl
committed
improvements to hasOne, hasMany, belongsTo to only get correct related data
1 parent 8c60a94 commit 73b5406

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

src/Database/Eloquent/Concerns/FMHasRelationships.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace BlueFeather\EloquentFileMaker\Database\Eloquent\Concerns;
55

66
use BlueFeather\EloquentFileMaker\Database\Eloquent\Relations\BelongsTo;
7+
use BlueFeather\EloquentFileMaker\Database\Eloquent\Relations\HasMany;
8+
use BlueFeather\EloquentFileMaker\Database\Eloquent\Relations\HasOne;
79
use Illuminate\Database\Eloquent\Builder;
810
use Illuminate\Database\Eloquent\Model;
911

@@ -58,12 +60,43 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
5860
* @param string $foreignKey
5961
* @param string $ownerKey
6062
* @param string $relation
63+
* @return BelongsTo
6164
*/
6265
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
6366
{
6467
// custom version of this so we can return our own BelongsTo class with a custom constraint for FM
6568
return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
6669
}
6770

71+
/**
72+
* Instantiate a new HasMany relationship.
73+
*
74+
* @param \Illuminate\Database\Eloquent\Builder $query
75+
* @param \Illuminate\Database\Eloquent\Model $parent
76+
* @param string $foreignKey
77+
* @param string $localKey
78+
* @return HasMany
79+
*/
80+
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
81+
{
82+
// custom version of this so we can return our own BelongsTo class with a custom constraint for FM
83+
return new HasMany($query, $parent, $foreignKey, $localKey);
84+
}
85+
86+
/**
87+
* Instantiate a new HasOne relationship.
88+
*
89+
* @param \Illuminate\Database\Eloquent\Builder $query
90+
* @param \Illuminate\Database\Eloquent\Model $parent
91+
* @param string $foreignKey
92+
* @param string $localKey
93+
* @return HasOne
94+
*/
95+
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
96+
{
97+
// custom version of this so we can return our own BelongsTo class with a custom constraint for FM
98+
return new HasOne($query, $parent, $foreignKey, $localKey);
99+
}
100+
68101

69102
}

src/Database/Eloquent/Relations/BelongsTo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function addConstraints()
1919
// of the related models matching on the foreign key that's on a parent.
2020
$table = $this->related->getTable();
2121

22-
$this->query->where($this->ownerKey, $this->child->{$this->foreignKey});
22+
$this->query->where($this->ownerKey, '=', $this->child->{$this->foreignKey});
2323
}
2424
}
2525
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
4+
namespace BlueFeather\EloquentFileMaker\Database\Eloquent\Relations;
5+
6+
7+
class HasMany extends \Illuminate\Database\Eloquent\Relations\HasMany
8+
{
9+
10+
/**
11+
* Set the base constraints on the relation query.
12+
*
13+
* @return void
14+
*/
15+
public function addConstraints()
16+
{
17+
if (static::$constraints) {
18+
$this->query->where($this->foreignKey, '=', $this->getParentKey());
19+
}
20+
}
21+
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace BlueFeather\EloquentFileMaker\Database\Eloquent\Relations;
5+
6+
7+
class HasOne extends \Illuminate\Database\Eloquent\Relations\HasOne
8+
{
9+
/**
10+
* Set the base constraints on the relation query.
11+
*
12+
* @return void
13+
*/
14+
public function addConstraints()
15+
{
16+
if (static::$constraints) {
17+
$this->query->where($this->foreignKey, '=', $this->getParentKey());
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)