2014-10-27 04:40:02 +01:00
title: Building Model and Search Interfaces around Scaffolding
2014-10-28 04:45:46 +01:00
summary: A Model-driven approach to defining your application UI.
2014-10-27 04:40:02 +01:00
# Scaffolding
The ORM already has a lot of information about the data represented by a `DataObject` through its `$db` property, so
2017-07-03 03:22:12 +02:00
SilverStripe will use that information to provide scaffold some interfaces. This is done though [FormScaffolder ](api:SilverStripe\Forms\FormScaffolder )
2014-10-27 04:40:02 +01:00
to provide reasonable defaults based on the property type (e.g. a checkbox field for booleans). You can then further
2015-01-13 20:59:55 +01:00
customise those fields as required.
2014-10-27 04:40:02 +01:00
## Form Fields
An example is `DataObject` , SilverStripe will automatically create your CMS interface so you can modify what you need.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
class MyDataObject extends DataObject
{
private static $db = [
'IsActive' => 'Boolean',
'Title' => 'Varchar',
'Content' => 'Text'
];
public function getCMSFields()
2017-08-07 05:11:17 +02:00
{
2017-10-27 04:38:27 +02:00
// parent::getCMSFields() does all the hard work and creates the fields for Title, IsActive and Content.
$fields = parent::getCMSFields();
$fields->dataFieldByName('IsActive')->setTitle('Is active?');
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
return $fields;
2017-08-07 05:11:17 +02:00
}
2017-10-27 04:38:27 +02:00
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2015-01-13 20:59:55 +01:00
To fully customise your form fields, start with an empty FieldList.
2014-10-28 04:45:46 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
public function getCMSFields()
{
$fields = FieldList::create(
TabSet::create("Root.Main",
CheckboxSetField::create('IsActive','Is active?'),
TextField::create('Title'),
TextareaField::create('Content')
->setRows(5)
)
);
return $fields;
}
2017-08-03 02:51:32 +02:00
```
2014-10-28 04:45:46 +01:00
2014-10-27 04:40:02 +01:00
You can also alter the fields of built-in and module `DataObject` classes through your own
2015-01-13 20:59:55 +01:00
[DataExtension ](/developer_guides/extending/extensions ), and a call to `DataExtension->updateCMSFields` .
2014-10-27 04:40:02 +01:00
## Searchable Fields
2015-01-13 20:59:55 +01:00
The `$searchable_fields` property uses a mixed array format that can be used to further customise your generated admin
2014-10-27 04:40:02 +01:00
system. The default is a set of array values listing the fields.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-05 00:45:24 +02:00
2017-10-27 04:38:27 +02:00
class MyDataObject extends DataObject
{
2017-08-03 05:35:09 +02:00
2017-10-27 04:38:27 +02:00
private static $searchable_fields = [
'Name',
'ProductCode'
];
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2017-07-03 03:22:12 +02:00
Searchable fields will be appear in the search interface with a default form field (usually a [TextField ](api:SilverStripe\Forms\TextField )) and a
default search filter assigned (usually an [ExactMatchFilter ](api:SilverStripe\ORM\Filters\ExactMatchFilter )). To override these defaults, you can specify
2014-10-27 04:40:02 +01:00
additional information on `$searchable_fields` :
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-05 00:45:24 +02:00
2017-10-27 04:38:27 +02:00
class MyDataObject extends DataObject
{
2017-08-03 05:35:09 +02:00
2017-10-27 04:38:27 +02:00
private static $searchable_fields = [
'Name' => 'PartialMatchFilter',
2017-12-06 10:37:25 +01:00
'ProductCode' => NumericField::class
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2017-07-03 03:22:12 +02:00
If you assign a single string value, you can set it to be either a [FormField ](api:SilverStripe\Forms\FormField ) or [SearchFilter ](api:SilverStripe\ORM\Filters\SearchFilter ). To specify
2014-10-27 04:40:02 +01:00
both, you can assign an array:
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
class MyDataObject extends DataObject
{
private static $searchable_fields = [
'Name' => [
2017-12-06 10:37:25 +01:00
'field' => TextField::class,
2017-10-27 04:38:27 +02:00
'filter' => 'PartialMatchFilter',
],
'ProductCode' => [
'title' => 'Product code #',
2017-12-06 10:37:25 +01:00
'field' => NumericField::class,
2017-10-27 04:38:27 +02:00
'filter' => 'PartialMatchFilter',
],
];
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
To include relations (`$has_one`, `$has_many` and `$many_many` ) in your search, you can use a dot-notation.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
class Team extends DataObject
{
private static $db = [
2017-08-07 05:11:17 +02:00
'Title' => 'Varchar'
2017-10-27 04:38:27 +02:00
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
private static $many_many = [
2017-08-07 05:11:17 +02:00
'Players' => 'Player'
2017-10-27 04:38:27 +02:00
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
private static $searchable_fields = [
'Title',
'Players.Name',
];
}
class Player extends DataObject
{
private static $db = [
2017-08-07 05:11:17 +02:00
'Name' => 'Varchar',
2017-10-27 04:38:27 +02:00
'Birthday' => 'Date',
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
private static $belongs_many_many = [
2017-08-07 05:11:17 +02:00
'Teams' => 'Team'
2017-10-27 04:38:27 +02:00
];
}
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
### Summary Fields
2017-07-03 03:22:12 +02:00
Summary fields can be used to show a quick overview of the data for a specific [DataObject ](api:SilverStripe\ORM\DataObject ) record. The most common use
is their display as table columns, e.g. in the search results of a [ModelAdmin ](api:SilverStripe\Admin\ModelAdmin ) CMS interface.
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
class MyDataObject extends DataObject
{
private static $db = [
2017-08-07 05:11:17 +02:00
'Name' => 'Text',
'OtherProperty' => 'Text',
'ProductCode' => 'Int',
2017-10-27 04:38:27 +02:00
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
private static $summary_fields = [
2017-08-07 05:11:17 +02:00
'Name',
2017-10-27 04:38:27 +02:00
'ProductCode',
];
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
To include relations or field manipulations in your summaries, you can use a dot-notation.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
class OtherObject extends DataObject
{
private static $db = [
'Title' => 'Varchar',
];
}
class MyDataObject extends DataObject
{
private static $db = [
2017-08-07 05:11:17 +02:00
'Name' => 'Text',
2017-10-27 04:38:27 +02:00
'Description' => 'HTMLText',
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
private static $has_one = [
'OtherObject' => 'OtherObject',
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
private static $summary_fields = [
2017-08-07 05:11:17 +02:00
'Name' => 'Name',
'Description.Summary' => 'Description (summary)',
2017-10-27 04:38:27 +02:00
'OtherObject.Title' => 'Other Object Title',
];
}
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
Non-textual elements (such as images and their manipulations) can also be used in summaries.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
class MyDataObject extends DataObject
{
private static $db = [
'Name' => 'Text',
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
private static $has_one = [
'HeroImage' => 'Image',
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
private static $summary_fields = [
2017-08-07 05:11:17 +02:00
'Name' => 'Name',
2017-10-27 04:38:27 +02:00
'HeroImage.CMSThumbnail' => 'Hero Image',
];
}
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2017-12-06 11:00:15 +01:00
In order to re-label any summary fields, you can use the $field_labels static.
```php
use SilverStripe\ORM\DataObject;
class MyDataObject extends DataObject
{
private static $db = [
'Name' => 'Text',
];
private static $has_one = [
'HeroImage' => 'Image',
];
private static $summary_fields = [
'Name',
'HeroImage.CMSThumbnail',
];
private static $field_labels = [
'Name' => 'Name',
'HeroImage.CMSThumbnail' => 'Hero',
];
}
2019-04-09 22:22:51 +02:00
```
2015-01-13 20:59:55 +01:00
## Related Documentation
2014-10-27 04:40:02 +01:00
* [SearchFilters ](searchfilters )
## API Documentation
2017-07-03 03:22:12 +02:00
* [FormScaffolder ](api:SilverStripe\Forms\FormScaffolder )
* [DataObject ](api:SilverStripe\ORM\DataObject )