Skip to content

Commit b70fdf5

Browse files
Merge remote-tracking branch 'origin/feature/relationship-resolution' into 2.x
2 parents d001c70 + 0b5c7dc commit b70fdf5

File tree

4 files changed

+77
-10
lines changed

4 files changed

+77
-10
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,27 @@ 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 the connections are set correctly, relationships from FMModel objects to sql-based Model objects should resolve correctly automatically. Relationships from regular `Model` objects to `FMModel` objects (version > 2.3.0 ) will require adding a new trait to your model class to enable the relationship to be created. For versions earlier than 2.3.0 or for more control over the relationship you can add a relationship connection manually using the examples below.
398398

399-
Here is an example of setting a native Laravel User Model to belong to a FileMaker-based Company FMModel class.
399+
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.
400+
401+
### Using trait to create a relationship (2.3.0+)
402+
The `HasHybridRelationships` trait allows the model to automatically resolve relationships from a `Model` to an `FMModel`. Here is an example of using the trait to create a native Laravel User `Model` in a SQL database to belong to a FileMaker-based Company `FMModel` class.
400403

401404
```php
402405
// User.php
403406

407+
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns\HasHybridRelationships;
408+
404409
class User extends Model
405410
{
411+
use HasHybridRelationships;
412+
406413
public function company()
407414
{
408-
return new \GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\BelongsTo(Company::query(), $this, 'company_id', 'id', '');
415+
// The Company class is an FMModel and is stored in FileMaker
416+
// The correct relationship will be resolved automatically thanks to the HasHybridRelationships trait
417+
return $this->belongsTo(Company::class, 'company_id', 'id');
409418
}
410419
}
411420
```
@@ -417,5 +426,24 @@ With this relationship created we can now get an FMModel of the Company the User
417426
$company = $user->company;
418427
```
419428

429+
### Manually creating a relationship
430+
431+
Using the `HasHybridRelationships` trait is the easiest way to create relationships between native Laravel models and FMModels. However, if you are using an older version of Eloquent FileMaker or want to manually manage the relationships you can establish the relationship by using the Eloquent FileMaker version of the relationship type. Each valid relationship type will be available under the `\GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\` namespace.
432+
433+
Here is an example of setting a native Laravel User Model to belong to a FileMaker-based Company FMModel class.
434+
435+
```php
436+
// User.php
437+
438+
class User extends Model
439+
{
440+
public function company()
441+
{
442+
// The Company class is an FMModel and is stored in FileMaker
443+
return new \GearboxSolutions\EloquentFileMaker\Database\Eloquent\Relations\BelongsTo(Company::query(), $this, 'company_id', 'id', '');
444+
}
445+
}
446+
```
447+
420448
## License
421449
Eloquent-FileMaker is open-sourced software licensed under the MIT license.

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+
}

src/Database/Eloquent/FMEloquentBuilder.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,16 @@
22

33
namespace GearboxSolutions\EloquentFileMaker\Database\Eloquent;
44

5-
use GearboxSolutions\EloquentFileMaker\Database\Eloquent\Concerns\FMHasRelationships;
65
use GearboxSolutions\EloquentFileMaker\Database\Query\FMBaseBuilder;
76
use GearboxSolutions\EloquentFileMaker\Exceptions\FileMakerDataApiException;
87
use Illuminate\Contracts\Support\Arrayable;
98
use Illuminate\Database\Eloquent\Builder;
109
use Illuminate\Database\Eloquent\Collection;
11-
use Illuminate\Database\Eloquent\Scope;
1210
use Illuminate\Pagination\Paginator;
1311
use Illuminate\Support\Arr;
1412

1513
class FMEloquentBuilder extends Builder
1614
{
17-
use FMHasRelationships;
18-
1915
/**
2016
* @return Collection
2117
*

0 commit comments

Comments
 (0)