Skip to content

Commit f0579c2

Browse files
likeadeckofcardsSmef
authored andcommitted
Add auto relationship resolution for FMModel and native Models
1 parent 241fcc9 commit f0579c2

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ It is possible to have relationships between native Laravel Model objects from y
394394
protected $connection = 'theConnectionName';
395395
```
396396

397-
Once they're set correctly, you can create relationships, such as a belongsTo, by manually creating a new eloquent-filemaker belongsTo object and setting the appropriate keys.
397+
Once they're set correctly, you can create relationships, such as a belongsTo, by manually creating a new eloquent-filemaker belongsTo object or importing a new trait and setting the appropriate keys.
398+
399+
### Manually creating a relationship
398400

399401
Here is an example of setting a native Laravel User Model to belong to a FileMaker-based Company FMModel class.
400402

@@ -410,6 +412,26 @@ class User extends Model
410412
}
411413
```
412414

415+
### Using trait to create a relationship (2.3.0+)
416+
Here is an example of using the trait to create a native Laravel User Model to belong to a FileMaker-based Company FMModel class.
417+
418+
```php
419+
// User.php
420+
421+
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns\HasHybridRelationships;
422+
423+
class User extends Model
424+
{
425+
use HasHybridRelationships;
426+
427+
public function company()
428+
{
429+
// This method looks at the related model and determines the correct relationship type
430+
return $this->belongsTo(Company::class, 'company_id', 'id');
431+
}
432+
}
433+
```
434+
413435
With this relationship created we can now get an FMModel of the Company the User belongs to like a normal relationship in a single database.
414436

415437
```php

src/Database/Eloquent/Concerns/FMHasRelationships.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22

33
namespace GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns;
44

5+
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\FMModel;
56
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\BelongsTo;
67
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\HasMany;
78
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\HasOne;
89
use Illuminate\Database\Eloquent\Builder;
10+
use Illuminate\Database\Eloquent\Concerns\HasRelationships;
911
use Illuminate\Database\Eloquent\Model;
1012

1113
trait FMHasRelationships
1214
{
15+
use HasRelationships {
16+
HasRelationships::hasOne as hasOneBase;
17+
HasRelationships::hasMany as hasManyBase;
18+
HasRelationships::newBelongsTo as newBelongsToBase;
19+
HasRelationships::newHasMany as newHasManyBase;
20+
HasRelationships::morphMany as morphManyBase;
21+
HasRelationships::newHasOne as newHasOneBase;
22+
}
23+
1324
/**
1425
* Define a one-to-one relationship.
1526
*
@@ -19,6 +30,10 @@ trait FMHasRelationships
1930
*/
2031
public function hasOne($related, $foreignKey = null, $localKey = null)
2132
{
33+
if (! ((new $related) instanceof FMModel)) {
34+
return $this->hasOneBase($related, $foreignKey, $localKey);
35+
}
36+
2237
$instance = $this->newRelatedInstance($related);
2338

2439
$foreignKey = $foreignKey ?: $this->getForeignKey();
@@ -38,6 +53,10 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
3853
*/
3954
public function hasMany($related, $foreignKey = null, $localKey = null)
4055
{
56+
if (! ((new $related) instanceof FMModel)) {
57+
return $this->hasManyBase($related, $foreignKey, $localKey);
58+
}
59+
4160
$instance = $this->newRelatedInstance($related);
4261

4362
$foreignKey = $foreignKey ?: $this->getForeignKey();
@@ -55,10 +74,14 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
5574
* @param string $foreignKey
5675
* @param string $ownerKey
5776
* @param string $relation
58-
* @return BelongsTo
77+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
5978
*/
6079
protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
6180
{
81+
if (! ($query->newModelInstance() instanceof FMModel)) {
82+
return $this->newBelongsToBase($query, $child, $foreignKey, $ownerKey, $relation);
83+
}
84+
6285
// custom version of this so we can return our own BelongsTo class with a custom constraint for FM
6386
return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
6487
}
@@ -68,10 +91,14 @@ protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $owne
6891
*
6992
* @param string $foreignKey
7093
* @param string $localKey
71-
* @return HasMany
94+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
7295
*/
7396
protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
7497
{
98+
if (! ($query->newModelInstance() instanceof FMModel)) {
99+
return $this->newHasManyBase($query, $parent, $foreignKey, $localKey);
100+
}
101+
75102
// custom version of this so we can return our own BelongsTo class with a custom constraint for FM
76103
return new HasMany($query, $parent, $foreignKey, $localKey);
77104
}
@@ -88,6 +115,10 @@ protected function newHasMany(Builder $query, Model $parent, $foreignKey, $local
88115
*/
89116
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
90117
{
118+
if (! ((new $related) instanceof FMModel)) {
119+
return $this->morphManyBase($related, $name, $type, $id, $localKey);
120+
}
121+
91122
$instance = $this->newRelatedInstance($related);
92123

93124
// Here we will gather up the morph type and ID for the relationship so that we
@@ -107,10 +138,14 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =
107138
*
108139
* @param string $foreignKey
109140
* @param string $localKey
110-
* @return HasOne
141+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
111142
*/
112143
protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
113144
{
145+
if (! ($query->newModelInstance() instanceof FMModel)) {
146+
return $this->newHasOneBase($query, $parent, $foreignKey, $localKey);
147+
}
148+
114149
// custom version of this so we can return our own BelongsTo class with a custom constraint for FM
115150
return new HasOne($query, $parent, $foreignKey, $localKey);
116151
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns;
4+
5+
trait HasHybridRelationships
6+
{
7+
use FMHasRelationships;
8+
}

0 commit comments

Comments
 (0)