mirror of https://github.com/vapor/docs.git
Merge pull request #68 from kdawgwilk/patch-1
Explain how to create many-to-many relationship
This commit is contained in:
commit
a2a29e5b27
|
|
@ -131,7 +131,7 @@ extension Pet {
|
|||
|
||||
And the opposite for `Toy`.
|
||||
|
||||
```
|
||||
```swift
|
||||
extension Toy {
|
||||
func pets() throws -> Siblings<Pet> {
|
||||
return try siblings()
|
||||
|
|
@ -146,6 +146,22 @@ let pet: Pet = ...
|
|||
let toys = pet.toys().all()
|
||||
```
|
||||
|
||||
To create a new many-to-many relationship you can do the following.
|
||||
|
||||
```swift
|
||||
|
||||
var toy: Toy = ... // Create a new toy
|
||||
try toy.save() // Save the toy to the db
|
||||
|
||||
|
||||
var pet: Pet = ... // Create a new pet
|
||||
try pet.save() // Save the pet to the db
|
||||
|
||||
// Link them together in the db
|
||||
var pivot = Pivot<Toy, Pet>(toy, pet) // Create the relationship
|
||||
try pivot.save() // Save the relationship to the db
|
||||
```
|
||||
|
||||
### Preparation
|
||||
|
||||
To prepare for a relationship with a `Pivot`, simply add the pivot to the `Droplet`'s preparations.
|
||||
|
|
|
|||
Loading…
Reference in New Issue