Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Manage the Category Values?


Reply


2 Posts   1321 Views

Avatar
SalvaStripe

Community Member, 89 Posts

15 May 2009 at 10:16pm

hey there,
is there a way to manage the Category values in the CMS?

this is a part of Resource.php
   static $db = array (
      'Name' => 'Text',
      'Description' => 'Text',
      'Category' => "Enum('Industry, Finance, Education')"
   );

is it able to ADD / REMOVE / RENAME these Category in the CMS? when there are entries belong to an category that i want to delete, these entries should switch to an "holder" or "exil" category, or the category cant be deleted while there are entries belong to.

hm.. someone? ;)

Avatar
UncleCheese

Forum Moderator, 4096 Posts

16 May 2009 at 3:35am

Of course. First of all, you know that's just example code, right? It's by no means anything you have to use.

If you want dynamic categories, you could do something like this:

class ResourcePage extends Page
{
static $has_many = array (
'Categories' => 'ResourceCategory'
);

// add to getCMSFields:
new TableField(
$this,
'Categories',
'ResourceCategory',
array('Name' => 'Name'),
array('Name' => 'TextField')
);
}

class ResourceCategory extends DataObject
{
static $db = array ('Name' => 'Varchar(50'));
static $has_one = array ('ResourcePage' => 'ResourcePage');
}

class Resource extends DataObject
{
static $has_one = array (
'Category' => 'ResourceCategory'
);
}
and then on your dropdown for categories, instead of the enumValues() function, just do this:

$map = ($records = $this->ResourcePage()->ResourceCategories()) ? $records : array();
$dropdown = new DropdownField('CategoryID','Resource category',$map);
$dropdown->setEmptyString('-- Select category --');

That's just pseudo code, but you get the idea.