Skip to content

Commit 6c23c18

Browse files
ryandialpadkiaking
andauthored
docs: add MorphTo documentation (#110)
Co-authored-by: Kia King Ishii <[email protected]>
1 parent 0caced0 commit 6c23c18

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

docs/guide/model/decorators.md

+26-6
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,35 @@ Marks a property on the model as a [morphOne attribute](../relationships/polymor
217217

218218
```ts
219219
import { Model, MorphOne } from '@vuex-orm/core'
220-
import Node from '@/models/Node'
220+
import Image from '@/models/Image'
221221

222-
class Cluster extends Model {
223-
static entity = 'clusters'
222+
class User extends Model {
223+
static entity = 'users'
224+
225+
@MorphOne(() => Image, 'imageableId', 'imageableType')
226+
image!: Image | null
227+
}
228+
```
229+
230+
### `@MorphTo`
231+
232+
Marks a property on the model as a [morphTo attribute](../relationships/polymorphic) type. For example:
233+
234+
```ts
235+
import { Model, MorphTo } from '@vuex-orm/core'
236+
import User from '@/models/User'
237+
import Post from '@/models/Post'
238+
239+
class Image extends Model {
240+
static entity = 'images'
224241

225242
@Attr(null)
226-
nodeIds!: number[]
243+
imageableId!: number | null
227244

228-
@HasManyBy(() => Image, 'imageableId', 'imageableType')
229-
image!: Image | null
245+
@Attr(null)
246+
imageableType!: string | null
247+
248+
@MorphTo(() => [User, Post], 'imageableId', 'imageableType')
249+
imageable!: User | Post | null
230250
}
231251
```

docs/guide/relationships/polymorphic.md

+51-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ A polymorphic relationship is where a model can belong to more than one type of
44

55
## One To One
66

7-
A one-to-one polymorphic relation is similar to a simple one-to-one relation; however, the target model can belong to
8-
more than one type of model on a single association. For example, an `Image` might be associated with a `User` or `Post`
9-
model.
7+
A one-to-one polymorphic relation is similar to a simple one-to-one relation; however, the target model can belong to more than one type of model on a single association. For example, an `Image` might be associated with a `User` or `Post` model.
108

119
### Defining A One To One Polymorphic Relationship
1210

13-
To define this relationship, for example, a `User` or `Post` model might be associated with one `Image`, we define a
14-
`morphOne` field to the `User` and `Post` models.
11+
To define this relationship, for example, a `User` or `Post` model might be associated with one `Image`, we define a `morphOne` field to the `User` and `Post` models.
1512

1613
```js
1714
class Image extends Model {
@@ -52,14 +49,10 @@ class Post extends Model {
5249
}
5350
```
5451

55-
The first argument passed to the `morphOne` method is the name of the model, the second argument is the name of the
56-
field which will contain the `id` of the model, and the third argument is the name of the field which will contain the
57-
`entity` of the parent model. The third argument is used to determine the "type" of the related parent model.
52+
The first argument passed to the `morphOne` method is the name of the model, the second argument is the name of the field which will contain the `id` of the model, and the third argument is the name of the field which will contain the `entity` of the parent model. The third argument is used to determine the "type" of the related parent model.
5853

5954
Additionally, Vuex ORM assumes that the foreign key should have a value matching the `id`
60-
(or the custom `static primaryKey`) field of the parent. In other words, Vuex ORM will look for the value of the user's
61-
`id` field in the `imageableId` field of the `Image` record. If you would like the relationship to use a value other
62-
than `id`, you may pass a fourth argument to the `morphOne` method specifying your custom key:
55+
(or the custom `static primaryKey`) field of the parent. In other words, Vuex ORM will look for the value of the user's `id` field in the `imageableId` field of the `Image` record. If you would like the relationship to use a value other than `id`, you may pass a fourth argument to the `morphOne` method specifying your custom key:
6356

6457
```js
6558
class User extends Model {
@@ -75,3 +68,50 @@ class User extends Model {
7568
}
7669
}
7770
```
71+
72+
### Defining The Inverse Of The Relationship
73+
74+
So, we can access the `Image` model from our `User` or `Post`. Now, let's define a relationship on the `Image` model that will let us access the model which owns the image. We can define the inverse of a `morphOne` relationship using the `morphTo` attribute:
75+
76+
```js
77+
class Image extends Model {
78+
static entity = 'images'
79+
80+
static fields () {
81+
return {
82+
id: this.number(0),
83+
url: this.string(''),
84+
imageableId: this.number(0),
85+
imageableType: this.string(''),
86+
imageable: this.morphTo(
87+
[User, Post],
88+
'imageableId',
89+
'imageableType'
90+
)
91+
}
92+
}
93+
}
94+
```
95+
96+
The first argument passed to the `morphTo` method is an array of models which are related, the second argument is the name of the field which will contain the `id` of the model, and the third argument is the name of the field which will contain the `entity` of the related model. The third argument is used to determine the "type" of the related model. You may also pass a fourth argument to the `morphTo` method specifying your custom key on the related model.
97+
98+
```js
99+
class Image extends Model {
100+
static entity = 'images'
101+
102+
static fields () {
103+
return {
104+
id: this.number(0),
105+
url: this.string(''),
106+
imageableId: this.number(0),
107+
imageableType: this.string(''),
108+
imageable: this.morphTo(
109+
[User, Post],
110+
'imageableId',
111+
'imageableType',
112+
'morphableId'
113+
)
114+
}
115+
}
116+
}
117+
```

0 commit comments

Comments
 (0)