【问题标题】:Silverstripe 4 - Dropdown field errorSilverstripe 4 - 下拉字段错误
【发布时间】:2018-08-23 02:22:00
【问题描述】:

我正在尝试创建一个下拉列表 (ss4.2),但我遗漏了一些东西,我不确定是什么。除了我需要的方法之外,我已经用其他方法取得了成功。有人能帮我弄清楚我错过了什么吗?

<?php
namespace SilverStripe\Gallery;

use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\DropdownField;
use SilverStripe\ORM\DBEnum;

class Album extends DataObject {

private static $db = [
   'AlbumType' => 'Enum(array("Type1","type2","Type3"), "Type1")',
];

private static $table_name = 'AlbumCoverPhoto';

public function getCMSFields() {
    $fields = FieldList::create(
        DropdownField::create('AlbumType',
            'Album type',
            singleton('Album')->dbObject('AlbumType')->enumValues())
        );

        return $fields;
    }   
 }

谢谢。 林恩

【问题讨论】:

    标签: php silverstripe silverstripe-4


    【解决方案1】:

    这只是这一行中的命名空间问题:

    singleton('Album')->dbObject('AlbumType')->enumValues())
    

    您要求的数据对象是 Album,而该类位于 SilverStripe\Gallery 命名空间内(旁注,请不要在 PHP 命名空间中使用 SilverStripe 作为供应商,因为它是为核心 SilverStripe 模块保留的和代码)。

    在引用类时,您应该尝试使用::class 表示法,例如

    singleton(Album::class)->dbObject('AlbumType')->enumValues()
    

    您实际上并不需要引用该类,因为无论如何您都在该类的范围内,因此您可以改用$this。试试这个:

    $this->dbObject('AlbumType')->enumValue()
    

    【讨论】:

    • 非常感谢您对使用命名空间的解释和建议。我已经做出了改变并且工作完美。
    • 你好,我正在浏览我的模板,刚刚意识到呈现的值是数字而不是数组 ["Type1","type2","Type3"] 中指定的值。它在管理界面中效果很好,但是我错过了将所选值呈现给模板的内容吗?不是 $AlbumType 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多