SilverStripe
外观
| 华夏公益教科书用户认为此页面应拆分为更小的页面,涵盖更窄的子主题。 你可以通过将此大页面拆分为更小的页面来提供帮助。请务必遵循命名规则。将书籍细分为更小的部分可以提供更多关注点,并允许每个部分专注于做好一件事,这对所有人都有益。 |
| 读者请求扩展本书,以包含更多内容。 你可以通过添加新内容(学习方法)或在阅览室中寻求帮助。 |

SilverStripe
本书旨在提供一套解决方案,用于解决 SilverStripe CMS(“内容管理系统”)中常见的难题。
SilverStripe 是基于 PHP5 和 MySQL5 的 MVC(模型-视图-控制器)框架和内容管理系统。
// controller
class Movies_Controller extends Page_Controller
{
// init
function init()
{
Requirements::javascript('mysite/javascript/movies.js');
parent::init();
}
}
假设我们需要允许管理员编辑“开发者”部分中的编码人员列表。
首先,我们需要创建“开发者”数据对象。
class Developers_DataObject extends DataObject
{
// fields
static $db = array(
'Name' =>'Varchar(150)',
'Lastname' =>'Varchar(150)',);
// edit form in the CMS
function getCMSFields_forPopup()
{
$fields = new FieldSet();
$fields->push( new TextField( 'Name', 'Name:' ) );
$fields->push( new TextField( 'Lastname', 'Lastname:' ) );
return $fields;
}
}
在同一个数据对象中,我们需要为表单定义字段。
接下来,我们需要为“开发者”部分创建页面类型。
class Developers_Page extends Page {
// make the relation between the section and the list of coders
static $has_one = array(
'MyDeveloper' => 'Developers_DataObject'
);
// manage the relation in the CMS
function getCMSFields()
{
// don't overwrite the main fields
$fields = parent::getCMSFields();
$tablefield = new HasOneComplexTableField(
$this,
'MyDeveloper',
'Developers_DataObject',
array(
'Name' => 'Name',
'Lastname' => 'Lastname',),
'getCMSFields_forPopup');
// add a new tab in the CMS
$fields->addFieldToTab( 'Root.Content.Coders', $tablefield );
return $fields;
}
}
我们的下一步是创建一个名为“开发者”的新页面。
现在,我们需要转到“行为”并更改页面类型为“开发者”。
此新页面将有一个名为“编码人员”的标签,我们可以在其中编辑开发者列表。
在这种情况下,我们将建立电影与城市的关联。然后,我们将允许管理员在“电影”页面类型中添加新城市。
<?php
/*
* Local Cities Data Object
*
*/
class LocalCitiesDataObject extends DataObject
{
static $db = array(
"Name" => "Varchar(150)"
);
// relation
static $has_one = array(
'LocalCities' => 'Movies' // 1-n relation
);
// edit form for the CMS
function getCMSFields_forPopup()
{
$fields = new FieldSet();
$fields->push( new TextField( 'Name' ) );
return $fields;
}
}
/**
* Defines the Movies page type
*/
class Movies extends Page
{
// relation
static $has_many = array(
'LocalCities' => 'LocalCitiesDataObject' // 1-n relation
);
// records list in the CMS
function getCMSFields()
{
// don't overwrite the defaults fields for the Page
$fields = parent::getCMSFields();
// for handling 1-n relation
$tablefield = new HasManyComplexTableField(
$this,
'LocalCities', // the name of the relationship
'LocalCitiesDataObject', // the related table
array(
"Name" => "Name"
),
'getCMSFields_forPopup' // the function to build the add/edit form
);
$fields->addFieldToTab( 'Root.Content.Cities', $tablefield );
return $fields;
}
}
此贡献由以下编码人员提供:
- Guy Steuart
- Leonardo Alberto Celis