0

我只是想在 OctoberCMS 后端建立一个关系,其中子表单应该从父表单中获取一个值作为建议(默认)。

详细地说,有两个模型通过 belongsTo 关系(参加者属于锦标赛)第一个父模型(field.yaml)覆盖体育锦标赛:

tournament:
    # ... some other fields
    start_date:
        label: Start date
        type: date
    attendees:
        label: Attendees
        type: partial

参加者的第二个儿童模型:

attendee:
    name:
        label: Name
        type: text
    start_date:
        label: Start date
        type: date
        # how to get the value from tournament->start_date as preset?

父表格(在体育比赛中)具有开始日期以及相关的部分涵盖参加比赛的运动员。每个运动员还有一个单独的开始日期字段,我想在父锦标赛表格调用此表格时预先填写比赛的开始日期。但它必须是可编辑的,因为并非所有与会者都会在同一天开始。

有一个内置函数可以使用来自同一表单的另一个字段的值预设一个字段,但不幸的是我找不到如何从父表单获取值的解决方案。

提前致谢!

4

1 回答 1

1

您可以将relationExtendManageWidget方法添加到您的Tournament controller并扩展其行为。您现在可以inject initial values for your new modal在创建新记录的同时。

// ..code
class Tournament extends Controller
{
    // ..code

    public function __construct()
    {
        // ..code
    }

    public function relationExtendManageWidget($widget, $field, $model)
    {
      // make sure we are doing on correct relation
      // also make sure our context is create so we only copy name on create
      if($field === 'attendees' 
          && property_exists($widget->config, 'context') 
          && $widget->config->context === 'create'
      ) {         
        $widget->config->model->start_date = $model->start_date;
        // this is attendee ^ model            ^ this is main tournament model
      }
    }
}

现在,当您尝试attendee record从您的 中创建新记录时Tournament record,您的参加者start_date将根据您的锦标赛进行预填充start_date,并且仅在创建新记录时才会发生。

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

于 2021-05-20T13:03:30.840 回答