Relations
Modelscan defineComputePropertywhich related to other Models.- These Relation
ComputeProperty'sCompiledComputeFunctionacceptfindOptionsas arguments, it allows filtering or ordering the records of the related Models by yourfindOptions. - For
Model, there are several function to create RelationComputeProperty
.belongsTo().hasMany().hasManyThrough()
TIP
For findOptions, please see findOptions.
belongsTo
| Arguments | type | default value |
|---|---|---|
| Related Model | Model | |
| Name of the FieldProperty of current Model | string | |
| Name of the FieldProperty of related Model | string | "id" |
export default class Product extends Model {
id = this.field(PrimaryKeyType)
shopId = this.field(NumberType)
//define a computeProperty as "belongsTo" relation
shop = Product.belongsTo(Shop, 'shopId', 'id')
}
export default class Shop extends Model {
id= this.field(PrimaryKeyType)
//define a computeProperty as "hasMany" relation
products = Shop.hasMany(Product, 'shopId', 'id')
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
hasMany
| Arguments | type | default value |
|---|---|---|
| Related Model | Model | |
| Name of the FieldProperty of related Model | string | |
| Name of the FieldProperty of current Model | string | "id" |
export default class Product extends Model {
id = this.field(PrimaryKeyType)
shopId = this.field(NumberType)
//define a computeProperty as "belongsTo" relation
shop = Product.belongsTo(Shop, 'shopId', 'id')
}
export default class Shop extends Model {
id= this.field(PrimaryKeyType)
//define a computeProperty as "hasMany" relation
products = Shop.hasMany(Product, 'shopId', 'id')
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12