Silverstripe Version:
SS 4.9
Question:
I wanted to understand how the variable type Enum works and if you can save the value from the Dropdown directly in the admin panel without adding a Varchar field in addition to the Enum (which I don’t show in the end of the day). I’ve seen discussion saying it was deprecated and that the data would be split in different objects, so I’m kinda confused if I should use it or not, and how.
This code works, basically I just have Dropdowns populated with Strings to choose which one to save from the back end and from a form in the front end. I already have both, but I can’t save the value in the back end without having the combination Enum + Varchar (it would go back to default value if I used only Enum like in SS3), and therefore not sure if I’m going in the right direction.
By the way, when doing Dev/Build with Enum, it throw an error, I have to first create the variable as another type like Int or Varchar, then once the table is created in the DB, I can switch to Enum and it works.
Details of your query go here
<?php
namespace App\Hub;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Member;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\DropdownField;
class PostObject extends DataObject {
private static $db = [
'Title' => 'Varchar',
'PostType' => 'Enum(["void","YouTube","Vimeo","Instagram","Image","Video"])',
'Type' => 'Varchar(20)',
'EmbedCode' => 'Varchar',
'IsActive' => 'Boolean(1)'
];
private static $defaults = [
'PostType' => 'void'
];
private static $has_one = [
'Member' => Member::class
];
public function getCMSFields(){
$fields = FieldList::create(TabSet::create('Root'));
//$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Main', [
TextField::create('Title'),
DropdownField::create( 'Type', 'Post Type', singleton('App\Hub\PostObject')->dbObject('PostType')->enumValues() )
]);
return $fields;
}
}