HAS_ONE(或HAS_MANY):外键在子关联对象中
例子:
父关联对象表:
product_id
product_name
子关联对象表:
image_id
image_name
product_id
外键是:product_id
父关联对象表:
product_id
class_id
product_name
子关联对象表:
class_id
class_name
Has many
- Related records are displayed as a list (view.list).
- Clicking a record will display an update form (manage.form).
- Clicking Add will display a selection list (manage.list).
- Clicking Create will display a create form (manage.form).
- Clicking Delete will destroy the record(s).
- Clicking Remove will orphan the relationship.
For example, if a Blog Post has many Comments, the target model is set as the blog post and a list of comments is displayed, using columns from the list definition. Clicking on a comment opens a popup form with the fields defined in form to update the comment. Comments can be created in the same way. Below is an example of the relation behavior configuration file:
# ===================================# Relation Behavior Config# ===================================
comments:
label: Comment
manage:
form: $/acme/blog/models/comment/fields.yaml
list: $/acme/blog/models/comment/columns.yaml
view:
list: $/acme/blog/models/comment/columns.yaml
toolbarButtons: create|delete
Belongs to many
- Related records are displayed as a list (view.list).
- Clicking Add will display a selection list (manage.list).
- Clicking Create will display a create form (manage.form).
- Clicking Delete will destroy the pivot table record(s).
- Clicking Remove will orphan the relationship.
For example, if a User belongs to many Roles, the target model is set as the user and a list of roles is displayed, using columns from the list definition. Existing roles can be added and removed from the user. Below is an example of the relation behavior configuration file:
# ===================================# Relation Behavior Config# ===================================
roles:
label: Role
view:
list: $/acme/user/models/role/columns.yaml
toolbarButtons: add|remove
manage:
list: $/acme/user/models/role/columns.yaml
form: $/acme/user/models/role/fields.yaml
Belongs to many (with Pivot Data)
- Related records are displayed as a list (view.list).
- Clicking a record will display an update form (pivot.form).
- Clicking Add will display a selection list (manage.list), then a data entry form (pivot.form).
- Clicking Remove will destroy the pivot table record(s).
Continuing the example in Belongs To Many relations, if a role also carried an expiry date, clicking on a role will open a popup form with the fields defined in pivot to update the expiry date. Below is an example of the relation behavior configuration file:
# ===================================# Relation Behavior Config# ===================================
roles:
label: Role
view:
list: $/acme/user/models/role/columns.yaml
manage:
list: $/acme/user/models/role/columns.yaml
pivot:
form: $/acme/user/models/role/fields.yaml
Pivot data is available when defining form fields and list columns via the pivot
relation, see the example below:
# ===================================# Relation Behavior Config# ===================================
teams:
label: Team
view:
list:
columns:
name:
label: Name
pivot[team_color]:
label: Team color
manage:
list:
columns:
name:
label: Name
pivot:
form:
fields:
pivot[team_color]:
label: Team color
Note: Pivot data is not supported by deferred bindings at this time, so the parent model should exist.
Belongs to
- Related record is displayed as a preview form (view.form).
- Clicking Create will display a create form (manage.form).
- Clicking Update will display an update form (manage.form).
- Clicking Link will display a selection list (manage.list).
- Clicking Unlink will orphan the relationship.
- Clicking Delete will destroy the record.
For example, if a Phone belongs to a Person the relation manager will display a form with the fields defined in form. Clicking the Link button will display a list of People to associate with the Phone. Clicking the Unlink button will dissociate the Phone with the Person.
# ===================================# Relation Behavior Config# ===================================
person:
label: Person
view:
form: $/acme/user/models/person/fields.yaml
toolbarButtons: link|unlink
manage:
form: $/acme/user/models/person/fields.yaml
list: $/acme/user/models/person/columns.yaml
Has one
- Related record is displayed as a preview form (view.form).
- Clicking Create will display a create form (manage.form).
- Clicking Update will display an update form (manage.form).
- Clicking Link will display a selection list (manage.list).
- Clicking Unlink will orphan the relationship.
- Clicking Delete will destroy the record.
For example, if a Person has one Phone the relation manager will display form with the fields defined in form for the Phone. When clicking the Update button, a popup is displayed with the fields now editable. If the Person already has a Phone the fields are update, otherwise a new Phone is created for them.
# ===================================# Relation Behavior Config# ===================================
phone:
label: Phone
view:
form: $/acme/user/models/phone/fields.yaml
toolbarButtons: update|delete
manage:
form: $/acme/user/models/phone/fields.yaml
list: $/acme/user/models/phone/columns.yaml