You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/relationships/polymorphic.md
+51-11
Original file line number
Diff line number
Diff line change
@@ -4,14 +4,11 @@ A polymorphic relationship is where a model can belong to more than one type of
4
4
5
5
## One To One
6
6
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.
10
8
11
9
### Defining A One To One Polymorphic Relationship
12
10
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.
15
12
16
13
```js
17
14
classImageextendsModel {
@@ -52,14 +49,10 @@ class Post extends Model {
52
49
}
53
50
```
54
51
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.
58
53
59
54
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:
63
56
64
57
```js
65
58
classUserextendsModel {
@@ -75,3 +68,50 @@ class User extends Model {
75
68
}
76
69
}
77
70
```
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
+
classImageextendsModel {
78
+
static entity ='images'
79
+
80
+
staticfields () {
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.
0 commit comments