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
Discriminated models can now be nested over multiple levels (e.g.
Document -> File -> Video). When hydrating a record, the type keys are
walked down the hierarchy and an instance of the most specific matching
type is created. make() now also resolves discriminated types, and
nested type models are registered to the database.
closes#1995
Copy file name to clipboardExpand all lines: docs/content/1.guide/2.model/7.single-table-inheritance.md
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,6 +196,72 @@ const people = useRepo(Person).all()
196
196
*/
197
197
```
198
198
199
+
### Nested Discriminators
200
+
201
+
Discriminated models can be nested over multiple levels. A derived model may define its own `typeKey` and `types()` map — when hydrating a record, Pinia ORM walks the type keys down the hierarchy and creates an instance of the most specific matching type.
202
+
203
+
```js
204
+
classAnimalextendsModel {
205
+
static entity ='animals'
206
+
207
+
statictypes () {
208
+
return {
209
+
animal: Animal,
210
+
dog: Dog,
211
+
}
212
+
}
213
+
214
+
staticfields () {
215
+
return {
216
+
id:this.attr(null),
217
+
type:this.attr('animal'),
218
+
}
219
+
}
220
+
}
221
+
222
+
classDogextendsAnimal {
223
+
static entity ='dogs'
224
+
225
+
static baseEntity ='animals'
226
+
227
+
// Second discriminator level with its own type key.
Note that if the `static fields` method doesn't expose the discriminator field (default or custom one), it will not be exposed in the results when fetching data. If you want to be able to read the discriminator field, you'll need to add it to the `fields` method **on the base entity**:
0 commit comments