The Relationship types

Relationship types
       How the relation manager is displayed depends on the relationship definition in the target model. The relationship type will also determine the configuration requirements, these are shown in  bold . The following relationship types are available:(以下是可用的)

HAS_ONE(或HAS_MANY):外键在子关联对象中
例子:
父关联对象表:
product_id
product_name
子关联对象表:
image_id
image_name
product_id
外键是:product_id


BELONGS_TO:外键在你父联对象中
父关联对象表:
product_id
class_id
product_name
子关联对象表:
class_id
class_name
外键是: class_id
     现在从Octobercms看来,在关联关系中,关系还是挺明确的,主从,外键,弄清楚就行了。
       

Has many

  1. Related records are displayed as a list (view.list).
  2. Clicking a record will display an update form (manage.form).
  3. Clicking Add will display a selection list (manage.list).
  4. Clicking Create will display a create form (manage.form).
  5. Clicking Delete will destroy the record(s).
  6. 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

  1. Related records are displayed as a list (view.list).
  2. Clicking Add will display a selection list (manage.list).
  3. Clicking Create will display a create form (manage.form).
  4. Clicking Delete will destroy the pivot table record(s).
  5. 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)

  1. Related records are displayed as a list (view.list).
  2. Clicking a record will display an update form (pivot.form).
  3. Clicking Add will display a selection list (manage.list), then a data entry form (pivot.form).
  4. 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

  1. Related record is displayed as a preview form (view.form).
  2. Clicking Create will display a create form (manage.form).
  3. Clicking Update will display an update form (manage.form).
  4. Clicking Link will display a selection list (manage.list).
  5. Clicking Unlink will orphan the relationship.
  6. 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

  1. Related record is displayed as a preview form (view.form).
  2. Clicking Create will display a create form (manage.form).
  3. Clicking Update will display an update form (manage.form).
  4. Clicking Link will display a selection list (manage.list).
  5. Clicking Unlink will orphan the relationship.
  6. 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
       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值