【问题标题】:Silverstripe has_one CMS dropdown filter before dropdown_field_thresholdSilverstripe has_one CMS 下拉过滤器在 dropdown_field_threshold 之前
【发布时间】:2019-03-06 10:53:29
【问题描述】:

我的一个模型中有一个has_one。但它超过了dropdown_field_threshold。 但是在getCMSFields 方法中,我更改了该下拉列表的查询。但是由于在调用此方法之前它已经超过了阈值,因此下拉列表将转换为 NumericDropdown 或类似的东西。

有没有办法在检查阈值之前更改查询?

顺便说一句,我知道我可以使用自己的查询创建自定义下拉列表。但是 Silverstripe 已经处理了很多,所以最好只更改查询。


编辑:示例代码

public function getCMSFields() {
    $fields = parent::getCMSFields();
    $questionnaire = $this->QuestionnaireSection()->Questionnaire();
    $nextQuestionOptions = $questionnaire->Sections();

    /** @var DropdownField $dropdownField */
    $dropdownField = $fields->dataFieldByName("NextQuestionID");
    $dropdownField->setSource($nextQuestionOptions->map()->toArray());

    return $fields;
}

【问题讨论】:

  • 能否分享您的代码?
  • @RobbieAverill 抱歉回复晚了,那天需要它,因为后天我在度假。我添加了我正在使用的代码。现在超出​​了阈值,因为所有“部分”都太多了,但我通过只获取当前“问卷”的部分来减少数量。所以我知道减少后它不应该超过阈值。

标签: silverstripe silverstripe-4


【解决方案1】:

不幸的是,无法在类的上下文中更改此查询。另一种方法可能是编写自己的脚手架函数而不是调用 parent::getCMSFields,但在这种情况下并不建议这样做。

如果您有某种方法可以全局获取当前的 Questionnaire 对象,您可以向 QuestionnaireSection 添加如下所示的 augmentDataQueryCreation 函数,以向正在执行的查询添加 where 子句。但请注意,每次执行 QuestionnaireSection::get() 时都会调用此函数。

public function augmentDataQueryCreation(SQLSelect $query, DataQuery $dataQuery){
    $baseTable = $this->baseTable();
    $filter = 1; //Your global param here
    $dataQuery->where("\"$baseTable\".\"QuestionnaireID\" = $filter");
}

防止 DropdownField 被 NumericField 替换的另一种方法是通过将以下代码添加到 mysite.yml 来更改此开关的阈值。这并不能完全解决您的问题,但却是一个很好的解决方法。

SilverStripe\ORM\FieldType\DBForeignKey:
    dropdown_field_threshold: 100000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多