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