首页 > 解决方案 > OcotberCMS PivotModel $attachOne 关系

问题描述

我正在尝试many-to-manyOcotberCMS.

这是我的关系

public $belongsToMany = [
    'users' => [
        User::class,
        'key' => 'task_id',
        'otherKey' => 'user_id',
        'table' => 'tasks_users',
        'pivot' => ['status'],
        'pivotModel' => TaskUser::class
    ]
];

在我的 pivotModel

class TaskUser extends Pivot
...
public $attachOne = [
    'file' => ['System\Models\File']
];

YAML 配置

pivot:
    form:
        tabs:
            fields:
                pivot[file]:
                    label: Image
                    type: fileupload
                    mode: image
                    span: left
                    tab: Image

表单正确呈现,但在尝试上传文件时,抛出错误: Upload error "A widget with class name 'relationUsersManagePivotFormPivotFile' has not been bound to the controller" on line 605 of D:\Projects\esport\modules\backend\classes\Controller.php

标签: phpmodel-view-controlleryamlrelationshipoctobercms

解决方案


似乎关系管理器无法处理很多嵌套。在枢轴模态中使用时也File不起作用。differed binding

differed binding不支持具有数据透视数据的关系管理器参考: https ://octobercms.com/docs/backend/relations#belongs-to-many-pivot

所以也许我们可以使用另一种方式。如果可能的话。

  1. file在数据透视表中,您可以添加新字段text type
  2. 在关系配置中指定它=>'pivot' => ['status', 'file']

现在从外地

pivot:
    form:
        fields:
            pivot[value]:
                label: Value
                type: text
            pivot[file]:
                label: Picture
                type: mediafinder                    

现在您可以上传和选择文件或选择现有文件。它们将被存储,file path以便可以直接访问。

如有任何疑问,请发表评论。


推荐阅读