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: adminforth/documentation/docs/tutorial/03-Customization/13-standardPagesTuning.md
+92-2Lines changed: 92 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -539,9 +539,9 @@ This way, when creating or editing a record you will be able to choose value for
539
539
540
540
When foreign resource column is not required, selector will have an 'Unset' option that will set field to `null`. You can change label for this option using `unsetLabel`, like so:
541
541
542
-
```typescript title="./resources/adminuser.ts"
542
+
```typescript title="./resources/apartments.ts"
543
543
exportdefault {
544
-
name: 'adminuser',
544
+
name: 'apartments',
545
545
columns: [
546
546
...
547
547
{
@@ -558,6 +558,96 @@ export default {
558
558
],
559
559
```
560
560
561
+
### Polymorphic foreign resources
562
+
563
+
Sometimes it is needed for one column to be a foreign key for multiple tables. For example, given the following schema:
564
+
565
+
```prisma title="./schema.prisma"
566
+
...
567
+
model apartments {
568
+
id String @id
569
+
created_at DateTime?
570
+
title String
571
+
square_meter Float?
572
+
price Decimal
573
+
number_of_rooms Int?
574
+
realtor_id String?
575
+
}
576
+
577
+
model houses {
578
+
id String @id
579
+
created_at DateTime?
580
+
title String
581
+
house_square_meter Float?
582
+
land_square_meter Float?
583
+
price Decimal
584
+
realtor_id String?
585
+
}
586
+
587
+
model sold_property {
588
+
id String @id
589
+
created_at DateTime?
590
+
title String
591
+
property_id String
592
+
realtor_id String?
593
+
}
594
+
595
+
```
596
+
597
+
Here, in `sold_property` table, column `property_id` can be a foreign key for both `apartments` and `houses` tables. If schema is set like this, the is no way to tell to what table exactly `property_id` links to. Also, if defined like usual, adminforth will link to only one of them. To make sure that `property_id` works as intended we need add one more column to `sold_property` and change the way foreign resource is defined in adminforth resource config.
598
+
599
+
```prisma title="./schema.prisma"
600
+
...
601
+
602
+
model sold_property {
603
+
id String @id
604
+
created_at DateTime?
605
+
title String
606
+
//diff-add
607
+
property_type String
608
+
property_id String
609
+
realtor_id String?
610
+
}
611
+
612
+
```
613
+
614
+
`property_type` column will be used to store what table id in `property_id` refers to. And in adminforth config for `sold_property` table, when describing `property_id` column, foreign resource field should be defined as follows:
When defined like this, adminforth will use value in `property_type` to figure out to what table does id in `property_id` refers to and properly link them. When creating or editing a record, adminforth will figure out to what table new `property_id` links to and fill `property_type` on its own using corresponding `whenValue`. Note, that `whenValue` does not have to be the same as `resourceId`, it can be any string as long as they do not repeat withing `polymorphicResources` array. Also, since `whenValue` is a string, column designated as `polymorphicOn` must also be string. Another thing to note is that, `polymorphicOn` column (`property_type` in our case) must not be editable by user, so it must include both `create` and `edit` as `false` in `showIn` value. Even though, `polymorphicOn` column is no editable, it can be beneficial to set is as an enumerator. This will have two benefits: first, columns value displayed in table and show page can be changed to a desired one and second, when filtering on this column, user will only able to choose values provided for him.
648
+
649
+
If `beforeDatasourceRequest` or `afterDatasourceResponse` hooks are set for polymorphic foreign resource, they will be called for each resource in `polymorphicResources` array.
errors.push(`Resource "${res.resourceId}" column "${col.name}" has foreignResource resourceId which is not in resources: "${col.foreignResource.resourceId}".
errors.push(`Resource "${res.resourceId}" column "${col.name}" has foreignResource polymorphicResource resourceId which is not in resources: "${polymorphicResource.resourceId}".
537
+
${similar ? `Did you mean "${similar}" instead of "${polymorphicResource.resourceId}"?` : ''}`);
errors.push(`Resource "${res.resourceId}" column "${col.name}" has foreignResource resourceId which is not in resources: "${col.foreignResource.resourceId}".
517
571
${similar ? `Did you mean "${similar}" instead of "${col.foreignResource.resourceId}"?` : ''}`);
0 commit comments