Skip to content

Commit 8cd75d1

Browse files
committed
docs update
1 parent f3dd6d2 commit 8cd75d1

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

docs/basic_usage.md

+29
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@ model(UserModel::class)->with('profile')->findAll();
3434

3535
This will perform two queries. One for all users, and one to fetch all the profiles for these users.
3636

37+
!!! note
38+
39+
You can still use your model as usual. If you omit the `with()` part, your model will work like a normal model.
40+
41+
### Writing with relation
42+
43+
For simple relations like `hasOne` or `hasMany`, you can also save the entire object that contains relations inside.
44+
45+
So this is also possible:
46+
47+
```php
48+
$userModel = model(UserModel::class);
49+
$user = $userModel->with('profile')->find(1);
50+
51+
$user->profile->favorite_pet = 'Cat';
52+
53+
$userModel->with('profile')->save($user);
54+
```
55+
56+
!!! note
57+
58+
If you use validation in the model, you should use a `useTransactions()` method to make sure that if something goes wrong, all changes are rolled back.
59+
60+
```php
61+
$userModel->with('profile')->useTransactions()->save($user);
62+
```
63+
64+
In general, if you are saving an object with a relation, you should always use this method.
65+
3766
## Lazy loading
3867

3968
Here we also have to specify our relations, just like in eager loading. The only difference is that we are required to use an `Entity` for our `$returnType`. That's because the entity will be responsible for triggering the relation request.

docs/configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ This library relies on traits.
2424

2525
### Entities
2626

27-
- Every entity should use `HasLazyRelations` trait.
27+
- Every entity should use `HasLazyRelations` trait - of course only if we want to use lazy loading.

docs/relations.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,23 @@ class UserModel extends Model
188188

189189
public function address(): Relation
190190
{
191-
return $this->hasOneThrough(AddressModel::class, CompanyModel::class);
191+
return $this->hasOneThrough(
192+
AddressModel::class,
193+
CompanyModel::class,
194+
'address_id', // foreignKey - AddressModel
195+
'id', // foreignKey - CompanyModel
196+
'id', // primaryKey - AddressModel
197+
'company_id', // primaryKey - CompanyModel
198+
);
192199
}
193200
}
194201
```
195202

203+
In this case we have to specify custom `$foreignKey` and `$primaryKey` for `CompanyModel` which will have the value of `id` and `company_id` respectively.
204+
205+
!!! note
206+
We can assign custom keys for all relations.
207+
196208
### Usage
197209

198210
```php

0 commit comments

Comments
 (0)