1
我按照https://www.silverstripe.org/learn/lessons/working-with-data-relationships-has-many?ref=hub上的教程創建了一些我的主頁的特色項目。但不知何故,我錯過了一塊,因爲我得到這個錯誤has_many和has_one關係SilverStripe
[錯誤]未捕獲的異常:沒有HAS_ONE階級「HomePageFeatured」發現,從「主頁」,以「HomePageFeatured」的的has_many關係需要在「HomePageFeatured」
一個HAS_ONEHomePage.php
<?php
/**
* Defines the HomePage page type
*/
class HomePage extends Page {
// private static $db = array(
//);
// private static $has_one = array(
//);
private static $has_many = array (
'Featured' => 'HomePageFeatured'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Featured', GridField::create(
'Featured',
'Hervorgehobene Produkte',
$this->Featured(),
GridFieldConfig_RecordEditor::create()
));
return $fields;
}
private static $icon = "themes/hstheme/images/treeicons/home";
}
class HomePage_Controller extends Page_Controller {
}
HomePageFeatured.php
<?php
/**
* Holds the featured items from the Homepage
*/
class HomePageFeatured extends DataObject {
private static $db = array(
'Title' => 'Varchar',
'Description' => 'Text'
);
private static $has_one = array(
'Photo' => 'Image',
'HomePageFeatured' => 'HomePageFeatured'
);
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('Title'),
TextareaField::create('Description'),
$uploader = UploadField::create('Photo')
);
$uploader->setFolderName('featured-photos');
$uploader->getValidator()->setAllowedExtensions(array('png','gif','jpeg','jpg'));
return $fields;
}
}
據我所知,這個問題是由公司首頁的$ HAS_ONE eFeatured.php。但它有一個來自HomePageFeatured的參考。
許多感謝的HAS_ONE!教程中有一個「暗示」:「通常情況下,相互之間的這種情況只能以父類命名。」不知道是否有關鍵字Parent。 – suntrop
@suntrop在這種情況下,'Parent''就是你的'HomePageFeatured'和你的'HomePage'之間的關係的名字。這個關係名稱可以是任何東西。我建議你小心使用'Parent'作爲關係的名字,因爲這已經是所有'頁面'上的一個關係,引用它是站點樹層次結構中的父項。因此,如果您的'HomePageFeatured'擴展了'Page'而不是'DataObject',那麼您可能會以'Parent''作爲關係名稱獲得意想不到的後果。本教程建議您這樣做:'private static $ has_one = ['HomePage'=>'HomePage']'。 –