Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Sorting products in category

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1333 Views
  • GXG2010
    Avatar
    Community Member
    4 Posts

    Sorting products in category Link to this post

    Hi UncleCheese,

    First of all I'd like to thank you for your effort to develop this module.
    I used your module and I think is fantastic.

    Now I have a little problem and I need a little help.
    I have created a Product object:

    class Product extends DataObject
    {
       static $db = array (
          "Name"         => "Text",
          "Description"   => "HTMLText",
       );
       static $has_one = array (
          "Thumbnail"   => "Image",
       );
       static $belongs_many_many = array (
          'ProductHolderPage' => 'ProductHolderPage'
       );
       

       public function getCMSFields_forPopup()
       {
          $fields = new FieldSet();
          $fields->push( new TextField('Name', 'Product name'));
          $fields->push( new ImageField(
             "Thumbnail",
             "Upload product image",
             null,
             null,
             null,
             "assets/Products/"
          ));
          $fields->push( new SimpleHTMLEditorField('Description','Product description', array ()));
          
          return $fields;
       }
       
    }

    and a ProductHolderPage

    class ProductHolderPage extends Page
    {
       static $db = array (
       );
       static $has_one = array (
       );
       static $has_many = array (
       );
       static $many_many = array (
          'Products' => 'Product'
       );
       static $icon = "themes/mpc/images/treeicons/productholder";
       static $can_be_root = false;
       static $allowed_children = "none";
          
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->removeFieldFromTab("Root.Content.Main","Content");
          
          # Products TAB
          $manager = new ManyManyDataObjectManager(
             $this,
             'Products',
             'Product',
             array('Name' => 'Product name'),
             'getCMSFields_forPopup'
          );
          $manager->setAddTitle('Product');
          $fields->addFieldToTab("Root.Content.Products", $manager);
          
          return $fields;
       }
       
    }

    Now I need to sort the Products in each ProductHolderPage not to sort the Products list.
    If I add the SortableDataObject::add_sortable_class('Product'); in the mysite/_config.php I can generally sort the Product's list but this is not useful in my case.

    I need to add sortOrder to the ProductHolderPage_Products table and to manage somehow in CMS.
    Is this possible?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Sorting products in category Link to this post

    You need SortableDataObject::add_many_many_sortable_relation()..

    It worked in 2.3.3, but when 2.3.4 came out, there was an update to Object.php that broke this functionality. I put in a bug report, and as far as I know, 2.4 will include the necessary patch to make it work again.

    See the thread "New Features" and read my post "many, many many_many new features"

  • GXG2010
    Avatar
    Community Member
    4 Posts

    Re: Sorting products in category Link to this post

    Hi UncleCheese,

    Thank you a lot! I still have trouble.

    I have SS 2.3.3 and DataObjectManager last version.
    I added in mysite/_config.php the line:

    SortableDataObject::add_sortable_many_many_relation('ProductHolderPage', 'Products');

    and I changed my class:

    class ProductHolderPage extends Page
    {
       static $db = array (
       );
       static $has_one = array (
       );
       static $has_many = array (
       );
       static $many_many = array (
          'Products' => 'Product'
       );
       static $icon = "themes/mpc/images/treeicons/productholder";
       static $can_be_root = false;
       static $allowed_children = "none";
          
       function getCMSFields() {
          $fields = parent::getCMSFields();
          $fields->removeFieldFromTab("Root.Content.Main","Content");
          
          # Products TAB
          $manager = new ManyManyDataObjectManager(
             $this,
             'Products',
             'Product',
             array('Name' => 'Product name'),
             'getCMSFields_forPopup'
          );
          $manager->setAddTitle('Product');
          $fields->addFieldToTab("Root.Content.Products", $manager);
    //      $manager->setOnlyRelated(true);
    //      $manager->setPermissions(array('edit'));
    //      $manager->setMarkingPermission(false);      
          return $fields;
       }
       
    }

    Everything it seems to be alright. I can check the "Allow drag & drop reordering" option and "Show only related records" but when I try to reorder the items, in the database the ProductHolderPage_Products table, SortOrder field, remains with the same values (0 zero default).

    Do you have any idea what I'm missing here?
    Thank you a lot for your help.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Sorting products in category Link to this post

    The changes weren't forward compatible, so I reverted them for 2.3.4+. To get the sort functionality working in 2.3.3, you'll have to use the doSort() function from r337

    class DataObjectManager_Controller extends Controller
    {
    function dosort()
    {
    if(!empty($_POST) && is_array($_POST) && isset($this->urlParams['ID'])) {
    $className = $this->urlParams['ID'];
    if(stristr($className,"-") !== false) {
    list($ownerClass, $className) = explode("-",$className);
    }
    $many_many = ((is_numeric($this->urlParams['OtherID'])) && SortableDataObject::is_sortable_many_many($className));
    foreach($_POST as $group => $map) {
    if(substr($group, 0, 7) == "record-") {
    if($many_many) {
    $controllerID = $this->urlParams['OtherID'];
    $candidates = singleton($ownerClass)->many_many();
    if(is_array($candidates)) {
    foreach($candidates as $name => $class)
    if($class == $className) {
    $relationName = $name;
    break;
    }
    }
    if(!isset($relationName)) return false;
    list($parentClass, $componentClass, $parentField, $componentField, $table) = singleton($ownerClass)->many_many($relationName);
    foreach($map as $sort => $id)
    DB::query("UPDATE `$table` SET SortOrder = $sort WHERE {$className}ID = $id AND {$ownerClass}ID = $controllerID");
    }
    else {
    foreach($map as $sort => $id) {
    $obj = DataObject::get_by_id($className, $id);
    $obj->SortOrder = $sort;
    $obj->write();
    }
    }
    break;
    }
    }
    }
    }
    }

  • GXG2010
    Avatar
    Community Member
    4 Posts

    Re: Sorting products in category Link to this post

    Thank you, Thank you, Thank you!

    Now everything is working perfectly. You are the best!

    1333 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.