Lynda Smith
Lynda Smith

Reputation: 49

Silverstripe 4 - Dropdown field error

I am trying to create a dropdown list (ss4.2) but I am missing something and I am not sure what. I have had success with other methods except the one I need. Would someone be able to help me figure out what I have missed?

<?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;
    }   
 }

Thanks. Lyn

Upvotes: 1

Views: 801

Answers (1)

scrowler
scrowler

Reputation: 24406

This is just an issue with namespacing in this line:

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

The data object you're asking for is Album, whereas the class is inside the SilverStripe\Gallery namespace (side note, please don't use SilverStripe as the vendor in your PHP namespaces as it's reserved for core SilverStripe modules and code).

When referring to classes you should try to use ::class notation, e.g.

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

You don't actually need to reference the class though, because you're inside the scope of that class anyway so you can use $this instead. Try this:

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

Upvotes: 3

Related Questions