2016-01-22 13 views
0

我正在建立一個在線商店。我正在嘗試通過使用單選按鈕(Silververstripe中的OptionsetField)來管理使用ModelAdmin來管理DataObject的has_one:has_many關係,但我有兩個問題。Silverstripe框架模型管理,數據對象關係和OptionsetField保存到數據庫中

  1. 當我點擊保存在CMS中時,關係值沒有保存到數據庫中。

  2. 狀態不會持續,因此當我下次登錄CMS時,可以看到上次選擇哪個單選按鈕。

接下來是我的代碼

----型號聯繫----

<?php 

class ProductAdmin extends ModelAdmin { 

    private static $menu_title = 'Store Manager'; 

    private static $url_segment = 'StoreManager'; 

    private static $managed_models = array (
     'Product'=>array('title'=>'All Store Products'), 
     'ProductCategory'=>array('title'=>'Product Categories'), 
     'ProductSubcategory'=>array('title' => 'Product Sub Categories') 
    ); 

    public $showImportForm = false; 

    } 

---- ----類

<?php 

    class ProductCategory extends DataObject { 

     private static $db = array (
      'Title' => 'Varchar', 
     ); 

     /** 
     * This relation links the Category to the Menu Items e.g. Category BoysClothing will be related to Boys * Menu Item. 
     */ 
     private static $has_one = array(
      'Page' => 'Page' 
     ); 

     private static $has_many = array (
      'Subcategories' => 'ProductSubcategory' 
     ); 
     public function getCMSFields(){ 
      $fields = FieldList::create(TabSet::create('Root')); 

      $fields->addFieldsToTab('Root.Main', array(
       TextField::create('Title', 'Name of Category'), 

       OptionsetField::create(
        'Page', //name 
        'Page this category belongs to', //title 
        SiteTree::get()->filter(array('ShowInMenus' => '1'))->map('ID', 'Title'), 
        1 
       ) 

      )//close array of fields 
      );//end adding fields to main tab 

      return $fields; 
     } 
} 

- - 產品子類別----

<?php 

    class ProductSubcategory extends DataObject { 

     private static $db = array (
      'Title' => 'Varchar', 
     ); 

     /** 
     * This relation links the Sub Category to the Category e.g. Category Khakis will be related to Boys 
     * Category 
     * 
     */ 
     private static $has_one = array(
      'Category' => 'ProductCategory' 
     );//will lead to creation of column BoysPageID on the table BoysCategory 

     private static $has_many = array (
      'Products' => 'Product' 
     ); 

     public function getCMSFields(){ 
      $fields = FieldList::create(TabSet::create('Root')); 

      $fields->addFieldsToTab('Root.Main', array(
       TextField::create('Title', 'Name of Sub Category'), 
       DropdownField::create(
        'Category', 
        'Category this subcategory belongs to', 
        ProductCategory::get()->map('ID', 'Title') 
       ) 


      )//close array of fields 
      );//end adding fields to main tab 

      return $fields; 
     } 


    }//end class 

    ---- Product ---- 
    <?php 

    class Product extends DataObject{ 
     private static $db = array (
      'Title'  => 'Varchar(255)', 
      'Price'  => 'Varchar', 
      'Colors' => 'Varchar', 
      'Sizes'  => 'Varchar', 
      'FeaturedOnHomepage' => 'Boolean' 
     ); 

     private static $has_one = array (
      'PrimaryPhoto' => 'Image', 
      'ProductSubcategory' => 'ProductSubcategory' 
     ); 


     static $summary_fields = array (
      'Title' => 'Name', 
      'Price' => 'Price', 
      'FeaturedOnHomepage.Nice' => 'Featured?', 
      'Thumbnail' => 'Picture' 
     ); 



     public function getCMSFields(){ 
      $fields = FieldList::create(TabSet::create('Root')); 

      $fields->addFieldsToTab('Root.Main', array(
       TextField::create('Title', 'Name of product'), 
       TextField::create('Price', 'Price of product'), 
       TextField::create('Colors', 'Available Colors'), //Make this a checkbox set or multiselect drop down 
       TextField::create('Sizes', 'Available Sizes'), //Make this a checkbox set or multiselect drop down 
       CheckboxField::create('FeaturedOnHomepage', 'Feature On HomePage'), 
       CheckboxSetField::create(
        'ProductSubcategory', 
        'Sub categories this product belongs to', 
        ProductSubcategory::get()->map('ID', 'Title') 
       ), 
       $primary_photo = UploadField::create('PrimaryPhoto', 'Product Primary Photo') 
      )//close array of fields 
      );//end adding fields to main tab 

      //Add other photos related to this image in a deifferent tab 
      $other_product_photos = UploadField::create('ProductImages', 'Product Photo'); 
      $fields->addFieldsToTab('Root.Other Photos', $other_product_photos); 

      //limit image extensions 
      $primary_photo->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif')); 
      $other_product_photos->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif')); 

      //set Folders 
      $primary_photo->setFolderName('ProductPhotos'); 
      $other_product_photos->setFolderName('ProductPhotos'); 


      //return 
      return $fields; 
     } 


     public function Link(){ 
      $sview_link = "prods/sview/"; 
      return $sview_link.$this->ID; 
     } 

     public function getThumbnail(){ 
      if($this->PrimaryPhoto()->ID){ 
       return $this->PrimaryPhoto()->setWidth(80); 
      }else{ 
       return 'No Image'; 
      } 
     } 


    }//close class Product 

回答

0

由於Page是has_one關係,所以您需要將後綴ID添加到formfield(是的,這對初學者很煩人)。

所以

  OptionsetField::create(
       'PageID', //name 
       'Page this category belongs to', //title 
       SiteTree::get()->filter(array('ShowInMenus' => '1'))->map('ID', 'Title'), 
       1 
      ) 

應該工作。 SubCategory中類別has_one相同。

你已經在你的代碼表示

//將導致對錶BoysCategory創建列BoysPageID的

這導致HAS_ONE關係美孚保存在DB作爲FooID,所以我們需要手動添加ID到表單域。