企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 扩展关系行为 ### [](https://octobercms.com/docs/backend/relations#extend-relation-behavior)扩展关系行为 有时您可能希望修改默认关系行为,并且有几种方法可以执行此操作。 * [扩展关系配置](https://octobercms.com/docs/backend/relations#extend-relation-config) * [扩展视图小部件](https://octobercms.com/docs/backend/relations#extend-view-widget) * [扩展管理小部件](https://octobercms.com/docs/backend/relations#extend-manage-widget) * [扩展枢轴小部件](https://octobercms.com/docs/backend/relations#extend-pivot-widget) * [扩展过滤器小部件](https://octobercms.com/docs/backend/relations#extend-filter-widgets) * [扩展刷新结果](https://octobercms.com/docs/backend/relations#extend-refresh-results) ### [](https://octobercms.com/docs/backend/relations#extend-relation-config)扩展关系配置 提供了操纵关系配置的机会。以下示例可用于根据模型的属性注入不同的columns.yaml文件。 ~~~ public function relationExtendConfig($config, $field, $model) { // Make sure the model and field matches those you want to manipulate if (!$model instanceof MyModel || $field != 'myField') return; // Show a different list for business customers if ($model->mode == 'b2b') { $config->view['list'] = '$/author/plugin_name/models/mymodel/b2b_columns.yaml'; } } ~~~ ### [](https://octobercms.com/docs/backend/relations#extend-view-widget)扩展视图小部件 提供了操作视图小部件的机会。 > **注意**:视图小部件尚未完全初始化,因此并非所有公共方法都能按预期工作!有关更多信息,请阅读[如何删除列](https://octobercms.com/docs/backend/relations#remove-column)。 例如,您可能想基于模型的属性切换showCheckboxes。 ~~~ public function relationExtendViewWidget($widget, $field, $model) { // Make sure the model and field matches those you want to manipulate if (!$model instanceof MyModel || $field != 'myField') return; if ($model->constant) { $widget->showCheckboxes = false; } } ~~~ #### [](https://octobercms.com/docs/backend/relations#remove-column)如何删除列 由于窗口小部件尚未在运行时循环的这一点完成初始化,因此无法调用$ widget-> removeColumn()。[ListController文档中](https://octobercms.com/docs/backend/lists#extend-list-columns)描述的addColumns()方法将按预期工作,但是要删除列,我们需要侦听RelationExtendViewWidget()方法中的“ list.extendColumns”事件。以下示例显示如何删除列: ~~~ public function relationExtendViewWidget($widget, $field, $model) { // Make sure the model and field matches those you want to manipulate if (!$model instanceof MyModel || $field != 'myField') return; // Will not work! $widget->removeColumn('my_column'); // This will work $widget->bindEvent('list.extendColumns', function () use($widget) { $widget->removeColumn('my_column'); }); } ~~~ ### [](https://octobercms.com/docs/backend/relations#extend-manage-widget)扩展管理小部件 提供了操作关系的管理小部件的机会。 ~~~ public function relationExtendManageWidget($widget, $field, $model) { // Make sure the field is the expected one if ($field != 'myField') return; // manipulate widget as needed } ~~~ ### [](https://octobercms.com/docs/backend/relations#extend-pivot-widget)扩展枢轴小部件 提供了操作关系的枢轴小部件的机会。 ~~~ public function relationExtendPivotWidget($widget, $field, $model) { // Make sure the field is the expected one if ($field != 'myField') return; // manipulate widget as needed } ~~~ ### [](https://octobercms.com/docs/backend/relations#extend-filter-widgets)扩展过滤器小部件 可以使用以下方法扩展两个过滤器小部件,一种用于的查看模式,一种用于的管理模式`RelationController`。 ~~~ public function relationExtendViewFilterWidget($widget, $field, $model) { // Extends the view filter widget } public function relationExtendManageFilterWidget($widget, $field, $model) { // Extends the manage filter widget } ~~~ 有关如何在过滤器小部件中以编程方式添加或删除范围的示例,请参见“[后端”列表文档](https://octobercms.com/docs/backend/lists#extend-filter-scopes)的“**扩展过滤器作用域”**部分。[](https://octobercms.com/docs/backend/lists#extend-filter-scopes) ### [](https://octobercms.com/docs/backend/relations#extend-refresh-results)扩展刷新结果 当管理窗口小部件进行更改时,视图窗口小部件通常会刷新,可以在发生此过程时使用此方法注入其他容器。返回带有额外值的数组以发送到浏览器,例如: ~~~ public function relationExtendRefreshResults($field) { // Make sure the field is the expected one if ($field != 'myField') return; return ['#myCounter' => 'Total records: 6']; } ~~~