Skip to content

Commit

Permalink
refacto: add polymorphism informations (#759)
Browse files Browse the repository at this point in the history
* refacto: add polymorphism informations

* fix: doc
  • Loading branch information
alexisvisco authored Jun 12, 2024
1 parent 1a09ba8 commit 461feed
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 93 deletions.
1 change: 1 addition & 0 deletions pages/_data/sidebar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ docs:
has_one: has_one.html
has_many: has_many.html
many_to_many: many_to_many.html
polymorphism: polymorphism.html
association_mode: associations.html
preload: preload.html
tutorials:
Expand Down
43 changes: 0 additions & 43 deletions pages/docs/has_many.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,49 +77,6 @@ type CreditCard struct {
}
```

## Polymorphism Association

GORM supports polymorphism association for `has one` and `has many`, it will save owned entity's table name into polymorphic type's field, primary key value into the polymorphic field

```go
type Dog struct {
ID int
Name string
Toys []Toy `gorm:"polymorphic:Owner;"`
}

type Toy struct {
ID int
Name string
OwnerID int
OwnerType string
}

db.Create(&Dog{Name: "dog1", Toys: []Toy{{Name: "toy1"}, {Name: "toy2"}}})
// INSERT INTO `dogs` (`name`) VALUES ("dog1")
// INSERT INTO `toys` (`name`,`owner_id`,`owner_type`) VALUES ("toy1","1","dogs"), ("toy2","1","dogs")
```

You can change the polymorphic type value with tag `polymorphicValue`, for example:

```go
type Dog struct {
ID int
Name string
Toys []Toy `gorm:"polymorphic:Owner;polymorphicValue:master"`
}

type Toy struct {
ID int
Name string
OwnerID int
OwnerType string
}

db.Create(&Dog{Name: "dog1", Toys: []Toy{{Name: "toy1"}, {Name: "toy2"}}})
// INSERT INTO `dogs` (`name`) VALUES ("dog1")
// INSERT INTO `toys` (`name`,`owner_id`,`owner_type`) VALUES ("toy1","1","master"), ("toy2","1","master")
```

## CRUD with Has Many

Expand Down
50 changes: 0 additions & 50 deletions pages/docs/has_one.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,56 +78,6 @@ type CreditCard struct {
}
```

## Polymorphism Association

GORM supports polymorphism association for `has one` and `has many`, it will save owned entity's table name into polymorphic type's field, primary key into the polymorphic field

```go
type Cat struct {
ID int
Name string
Toy Toy `gorm:"polymorphic:Owner;"`
}

type Dog struct {
ID int
Name string
Toy Toy `gorm:"polymorphic:Owner;"`
}

type Toy struct {
ID int
Name string
OwnerID int
OwnerType string
}

db.Create(&Dog{Name: "dog1", Toy: Toy{Name: "toy1"}})
// INSERT INTO `dogs` (`name`) VALUES ("dog1")
// INSERT INTO `toys` (`name`,`owner_id`,`owner_type`) VALUES ("toy1","1","dogs")
```

You can change the polymorphic type value with tag `polymorphicValue`, for example:

```go
type Dog struct {
ID int
Name string
Toy Toy `gorm:"polymorphic:Owner;polymorphicValue:master"`
}

type Toy struct {
ID int
Name string
OwnerID int
OwnerType string
}

db.Create(&Dog{Name: "dog1", Toy: Toy{Name: "toy1"}})
// INSERT INTO `dogs` (`name`) VALUES ("dog1")
// INSERT INTO `toys` (`name`,`owner_id`,`owner_type`) VALUES ("toy1","1","master")
```

## CRUD with Has One

Please checkout [Association Mode](associations.html#Association-Mode) for working with `has one` relations
Expand Down
58 changes: 58 additions & 0 deletions pages/docs/polymorphism.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: Polymorphism
layout: Page
---

## Polymorphism Association

GORM supports polymorphism association for `has one` and `has many`, it will save owned entity's table name into polymorphic type's field, primary key value into the polymorphic field

By default `polymorphic:<value>` will prefix the column type and column id with `<value>`.
The value will be the table name pluralized.

```go
type Dog struct {
ID int
Name string
Toys []Toy `gorm:"polymorphic:Owner;"`
}

type Toy struct {
ID int
Name string
OwnerID int
OwnerType string
}

db.Create(&Dog{Name: "dog1", Toys: []Toy{{Name: "toy1"}, {Name: "toy2"}}})
// INSERT INTO `dogs` (`name`) VALUES ("dog1")
// INSERT INTO `toys` (`name`,`owner_id`,`owner_type`) VALUES ("toy1",1,"dogs"), ("toy2",1,"dogs")
```


You can specify polymorphism properties separately using the following GORM tags:

- `polymorphicType`: Specifies the column type.
- `polymorphicId`: Specifies the column ID.
- `polymorphicValue`: Specifies the value of the type.

```go
type Dog struct {
ID int
Name string
Toys Toy `gorm:"polymorphicType:Kind;polymorphicId:OwnerID;polymorphicValue:master"`
}

type Toy struct {
ID int
Name string
OwnerID int
Kind string
}

db.Create(&Dog{Name: "dog1", Toys: []Toy{{Name: "toy1"}, {Name: "toy2"}}})
// INSERT INTO `dogs` (`name`) VALUES ("dog1")
// INSERT INTO `toys` (`name`,`owner_id`,`kind`) VALUES ("toy1",1,"master"), ("toy2",1,"master")
```

In these examples, we've used a has-many relationship, but the same principles apply to has-one relationships.
1 change: 1 addition & 0 deletions themes/navy/languages/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ sidebar:
belongs_to: Belongs To
has_one: Has One
has_many: Has Many
polymorphism: Polymorphism
many_to_many: Many To Many
association_mode: Association Mode
preload: Preloading (Eager Loading)
Expand Down

0 comments on commit 461feed

Please sign in to comment.