# 擴展關系行為
### [](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'];
}
~~~
- 基本說明
- 基本操作
- October cms 安裝
- 后臺控制器路徑
- 圖標
- 獲取安裝網上的插件/主題
- 插件構造器使用
- 定時任務
- October后臺控制器
- vscode編輯器
- ajax操作
- 使用
- ajax更新組件
- ajax屬性API
- JavaScript API
- ajax綜合使用
- 主題
- 多語言主題
- 安裝市場主題
- 主題程序處理
- 主題
- 頁面
- 部件
- 布局
- 內容
- 組件
- 媒體
- 主題表單操作
- 表單使用
- 表單后端程序處理
- 插件
- 自定義插件
- 插件說明
- 插件導航條
- 插件數據庫設置
- 插件的設置管理
- 插件的配置文件config
- 組件
- app服務
- app容器
- 擴展行為
- 緩存
- Collection類
- Lazy Collections
- Collection方法
- 助手函數
- 數組助手函數
- 路徑助手函數
- 玄樂助手函數
- 其他助手函數
- 錯誤與記錄
- 事件處理
- HTML頁面
- 文件與目錄操作
- 散列和加密
- 郵件
- 郵件內容
- 郵件發送
- 分頁
- 模板解析器
- 動態解析器語法
- 隊列消息
- 請求與輸入
- 響應
- 視圖
- 路由器
- 配置
- 驗證操作
- 處理錯誤消息
- 錯誤消息與視圖
- 可用的驗證規則
- 有條件的驗證規則
- 驗證數組
- 錯誤消息
- 自定義驗證規則
- 模型操作
- 定義模型與其屬性
- 檢索模型
- 插入與更新
- 刪除模型
- 查詢范圍
- 事件操作
- 關聯操作
- 定義關系
- 關系類型
- 多肽關系
- 關系查詢
- 渴望加載
- 插入模型
- 數據庫操作
- 基本用法
- 數據表結構
- 查詢連貫操作
- 結果檢索
- select子句
- 插入更新
- where子句
- 排序,分組,限制和偏移
- 文件附件
- Collection操作
- 屬性操作
- 系列化json
- 數據庫屬性
- 數據庫行為
- 控制器
- 后臺控制器定義
- 后臺頁面
- 后臺組件
- 后臺表單
- 表單組件
- 表單視圖
- 表單行為
- 后臺列表
- 列表行為
- 列表過濾器
- 可用列類型
- 關系行為
- 關系行為類型
- 擴展關系行為
- 列表排序操作
- 導入導出操作
- 用于與權限
- corlate模板修改
- 修改頂部導航
- laravel問題
- 控制器不存在
- 控制器
- 路由組
- laravel筆記
- laravel 安裝
- 偽靜態配置
- 依賴注入 & 控制器
- 中間件
- 路由文件
- 視圖