This document contains information for an outdated version (2.4) and may not be maintained any more.

If some of your projects still use this version, consider upgrading as soon as possible.

ModelAdmin

Introduction

Replaces GenericDataAdmin in Silverstripe 2.3

The ModelAdmin provides a simple way to utilize the SilverStripe CMS UI with your own custom data models. The ModelAdmin uses the DataObject's Scaffolding to create the search fields, forms, and displayed data within the CMS.

In order to customize the ModelAdmin CMS interface you will need to understand how DataObject works.

Requirements

Requires Silverstripe 2.3

Usage

Step 1

Extend ModelAdmin with a custom class for your admin area, and edit the $managed_models property with the list of data objects you want to scaffold an interface for:

class MyCatalogAdmin extends ModelAdmin {
   
  public static $managed_models = array(   //since 2.3.2
      'Product',
      'Category'
   );

  static $url_segment = 'products'; // will be linked as /admin/products
  static $menu_title = 'My Product Admin';

}

To add the ModelAdmin to your CMS menu, you simply need to define a couple of statics on your ModelAdmin subclass. See LeftAndMain on how to make your menu title translatable.

Step 2

Add a $searchable_fields (See ModelAdmin::$searchable_fields) property to your data models, to define the fields and filters for the search interface:

Datamodel Product:

class Product extends DataObject {

   static $db = array(
      'Name' => 'Varchar',
      'ProductCode' => 'Varchar',
      'Description' => 'Text',
      'Price' => 'Currency'
   );

   static $has_one = array(
      'Category' => 'Category'
   );

   static $searchable_fields = array(
      'Name',
      'ProductCode' 
   );

}

Datamodel Category:

<?php
class Category extends DataObject {
   static $db = array(
      'Title' => 'Text'
   );
}
?>

Step 3

You can now log in to the main CMS admin and manage your data objects, with no extra implementation required.

Note about has_one

Scaffolding has_one relationships in your ModelAdmin relies on a column in the related model to be named Title or Name of a string type (varchar, char, etc). These will be pulled in to the dropdown when creating a new object.

If you are seeing a list of ID#s when creating new objects, ensure you have one of those two in the related model.

Searchable Fields

You can customize the fields which are searchable for each managed DataObject class, as well as the ways in which the fields are searched (e.g. "partial match", "fulltext", etc.) using $searchable_fields.

Summary Fields

Summary Fields are the columns which are shown in the TableListField when viewing DataObjects. These can be customized for each DataObject's search results using $summary_fields.

Comments

Comment policy: Please use comments for tips and corrections about the described functionality.
Comments are moderated, we reserve the right to remove comments that are inappropriate or are no longer relevant. Use the Silverstripe Forum to ask questions.

blog comments powered by Disqus