【问题标题】:Silverstripe Framework Model Admin, Data Object Relations and OptionsetField saving into the DatabaseSilverstripe 框架模型管理、数据对象关系和选项集字段保存到数据库中
【发布时间】:2016-04-28 20:09:37
【问题描述】:

我正在建立一个在线商店。我正在尝试实现 has_one : has_many 关系的 DataObjects 正在使用 ModelAdmin 并使用单选按钮(Silverstripe 中的 OptionsetField)进行管理,但我有 2 个问题。

  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

【问题讨论】:

    标签: silverstripe modeladmin


    【解决方案1】:

    由于 Page 是 has_one 关系,您需要将后缀 ID 添加到表单域(是的,这对初学者来说很烦人)。

    所以

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

    应该可以。 SubCategory 中的 Category has_one 也是如此。

    你已经在你的代码中声明了

    //将导致在表 BoysCategory 上创建列 BoysPageID

    这是因为 has_one 关系 Foo 作为 FooID 保存在 DB 中,所以我们需要手动将 ID 添加到表单字段中。

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 2020-10-12
      • 2017-02-22
      • 1970-01-01
      • 2017-10-07
      相关资源
      最近更新 更多