Jump to:

5519 Posts in 1734 Topics by 1223 members

Customising the CMS

SilverStripe Forums » Customising the CMS » [Solved] Saving a $many_many in a dropdown field

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 926 Views
  • Graphicator
    Avatar
    Community Member
    62 Posts

    [Solved] Saving a $many_many in a dropdown field Link to this post

    I hit a wall with saving the selected DropdownField while editing a Page. I'm able to grab the different titles, but nothing is saved.

    I want to select one Title Column that is inside the Brands class extending DataObject.
    I successfully implemented the dropdown field as set up in Chapter 6 of Silverstripe book to use as a search field. This time I'm applying to same way of generating the field but for adding it to a Page's field.

    Item.php

    public static $many_many = array(
          'Brands' => 'Brand'
       );

       function getCMSFields() {
    ...
       $brands = Product::get_brands();
          if($brands){
             $brandMap = array_combine($brands,$brands);
          } else {
             $brandMap = null;
          }
          $brandField = new DropdownField(
             'Brand',
             'Brand',
             $brandMap
          );
          $brandField->setHasEmptyDefault(true);
          
          $fields->addFieldToTab('Root.Content.Main',$brandField);

    Item.php cont'd

       # 6.7.3 Drop-down Menu for Searching brands
       public function getDefaultSearchContext() {
          $context = parent::getDefaultSearchContext();
          $context->removeFieldByName('Brand');
          $brands = self::get_brands();
          if($brands){
             $brandMap = array_combine($brands,$brands);
          } else {
             $brandMap = null;
          }
          $brandField = new DropdownField(
             'Brand',
             'Brand',
             $brandMap
          );
          $brandField->setHasEmptyDefault(true);
          $context->addField($brandField);
          return $context;
       }
       
       public static function get_brands() {
          $comp = DataObject::get('Brand');
          if(!$comp) return array();
          $brands = array_unique($comp->column('Title'));
          sort($brands);
          return $brands;
       }

    Brand.php

    class Brand extends DataObject {
       static $db = array(
          'Title' => 'Varchar(255)' ,
       );
       static $belongs_many_many = array(
          'Products' => 'Product',
       );
       
       static $searchable_fields = array(
          'Title' => 'ExactMatchFilter'
       );
       
    }

  • Graphicator
    Avatar
    Community Member
    62 Posts

    Re: [Solved] Saving a $many_many in a dropdown field Link to this post

    After banging my head a few times, I realized I was missing componentset.

    Really needed to look at the datamodel page: http://doc.silverstripe.org/sapphire/en/topics/datamodel

    And decided to comment out the belongs_many_many to a has_many for each brand.

    Brand.php

       # static $belongs_many_many = array(
       #   'Products' => 'Product',
       # );
       static $has_many = array(
          'Products' => 'Product.Brands',
       );

    The dropdown is saving now that I used
    Product.php

       function getCMSFields() {
    ....
       $brandMap = Dataobject::get("Brand");
          $brandArray = $brandMap ->toDropdownMap("ID", "Title",'',true);
          $brandField = new DropdownField(
             'BrandsID',
             'Please choose a brand',
             $brandArray
          );
          $brandField->setHasEmptyDefault(true);
          $fields ->addFieldToTab('Root.Content.Main',$brandField,'Content');

    Thank you!

    926 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.