relation - 根据字段关系类型,渲染为下拉列表或复选框列表。单一关系显示一个下拉列表,多重关系显示一个复选框列表。用于显示每个关系的标签来源于 nameFrom 或 select 定义。
categories:
label: Categories
type: relation以下 字段属性 受支持且常用。
| Property | Description |
|---|---|
| label | a name when displaying the form field to the user. |
| comment | places a descriptive comment below the field. |
| nameFrom | a model attribute name used for displaying the relation label. Default: name. |
| excludeFrom | a parent model attribute used to exclude related keys in the list, optional. |
| select | a custom SQL select statement to use for the name. |
| emptyOption | text to display when there is no available selections. |
| conditions | specifies a raw where query statement to apply to the model query. |
| modelScope | applies a model query scope method to the related form model, can be a model method name or a static PHP class method (Class::method). |
| defaultSort | sets a default sorting column and direction, supports a string for the column name or an array with keys column and direction. The direction can be asc for ascending (default) or desc for descending order. |
| useController | automatically detects if this field configured with Relation Controller behavior and use it. Default: true |
| controller | specifies an array to manually configure integration with the Relation Controller behavior. |
使用 nameFrom 属性来自定义关联记录所使用的标签。
categories:
label: Categories
type: relation
nameFrom: title或者,您可以使用自定义的 select 语句填充该标签。任何有效的 SQL 语句在此处均可使用。
user:
label: User
type: relation
select: concat(first_name, ' ', last_name)您可以使用以下方法,通过SQL或PHP条件过滤可用记录。
您可以限制相关模型通过原始 SQL 查询使用 conditions 属性。
user:
label: User
type: relation
conditions: is_featured = true该值也支持从父模型属性中解析的简单参数。参数名称以冒号 (:) 字符开头。
country:
label: Country
type: relation
state:
label: State
type: relation
dependsOn: country
conditions: custom_country_id = :country_id您可以提供一个模型范围,用于通过 modelScope 属性过滤结果。
user:
label: User
type: relation
modelScope: withTrashedmodelScope 可以用来连接两个相关字段,例如,连接 Country 和 State 模型,其中可用状态会根据所选国家进行筛选。 dependsOn 属性启用 字段依赖,并在选中 country 时更新 state 选项。
country:
label: Country
type: relation
state:
label: State
type: relation
dependsOn: country
modelScope: filterStatesmodelScope 值 filterStates 对应于 State 模型中定义的 scopeFilterStates 方法。提供给 模型查询范围 的 $model(第二个参数)让您捕获所选国家并筛选可用选项。
public function scopeFilterStates($query, $model)
{
if ($countryId = $model->country_id) {
$query->where('country_id', $countryId);
}
}如果控制器实现了关联控制器行为并且该字段在那里定义,那么它将使用此定义显示。将useController 属性设置为 false 以禁用此功能。
countries:
label: Categories
type: relation
useController: falsecontroller 属性可用于指定内联配置。
products:
label: Products
tab: Products
type: relation
controller:
label: Product
list: $/october/test/models/product/columns.yaml
form: $/october/test/models/product/fields.yaml