has_many和has_one关系SilverStripe

时间:2016-12-07 10:04:30

标签: php silverstripe

我按照https://www.silverstripe.org/learn/lessons/working-with-data-relationships-has-many?ref=hub上的教程为我的主页创建了一些精选项目。但不知怎的,我错过了一件,因为我得到了这个错误

[错误]未捕获异常:在'HomePageFeatured'类中找不到has_one,从'HomePage'到'HomePageFeatured'的has_many关系需要'HomePageFeatured'上的has_one

HomePage.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;
    }

}

据我了解,问题是来自HomePageFeatured.php的$ has_one。但它有来自HomePageFeatured的参考。

1 个答案:

答案 0 :(得分:1)

HomePageFeatured需要一个'HomePage'

的has_one
class HomePageFeatured extends DataObject {
   private static $has_one = array(
        'Photo' => 'Image',
        'Parent' => 'HomePage',
   );
}