Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Linking list of Teasers (TreeMultiselectField)


Go to End


3 Posts   2451 Views

Avatar
maxx

Community Member, 4 Posts

10 May 2010 at 11:42pm

Hello!

On my Homepage (PageType: HomePage) I would like to have a list of Teasers to use. Each Teaser is defined in a separate entry (PageType: HPTeaser) in the Content-Tree. The idea is to use a teaser on several pages, being able to change the content of the teaser in a single place.

I used the following code in my HomePage.php file:

class HomePage extends Page {
   
   public static $db = array(
   );
   
   public static $has_many = array(
      "HPTeasers" => "HPTeaser"
   );
   
   
   
   function getCMSFields() {
      $fields = parent::getCMSFields();
      
      $hpteaserfield = new TreeMultiselectField(
         "HPTeasers",
         "Homepage Teasers",
         "HPTeaser"
      );   
         

      $fields->addFieldToTab("Root.Content.Main", $hpteaserfield);
            
      return $fields;
   }
   
}

The DropDown is correctly displayed while editing a page of type HomePage, but I have the following problems:

- The selection is not limited to entries of type HPTeaser
- When I select one ore more entries they are listed correctly, but nothing seems to be saved to the database (when I reload the editor the field HPTeasers is always empty

So what could I do to fix these problems? And is there a better FieldType to use (eg. allowing Drag&Drop reordering of the selected Teasers)?

Many Thanks for your help

Maxx

Avatar
rob.s

Community Member, 78 Posts

20 May 2010 at 5:57am

Edited: 20/05/2010 5:58am

Hi Maxx,

i think you need different relations. Because ONE Teaser can be related on many Pages.
And many Pages can have many Teasers - is that right ?

class HomePage extends Page {
  static $many_many = array(
    "Teasers" => "HPTeaser",
  );
}
class HPTeaser extends Page {
  static $belongs_many_many = array(
    "HomePages" => "HomePage",
  );
}

If you want a orderrable n:m relation, take a look at the outstanding DataObjectManger

In your case especially the ManyManyDataObjectManager while using:
/mysite/_config.php

// ->  mysite/_config.php
SortableDataObject::add_sortable_many_many_relation('Teasers', 'HPTeaser');

Avatar
Tonyair

Community Member, 81 Posts

21 July 2010 at 11:45pm

Hello, I have a problem with many_many sortable.

SortableDataObject::add_sortable_many_many_relation('WarePage','WareItems');

WarePage get items function:
$doSet = $this->getManyManyComponents('WareItems', '','', '', "{$SQL_start},10");

Returns error:

[User Error] Couldn't run query: SELECT count(*) FROM SELECT "WareItem"."ClassName", "WareItem"."Created", "WareItem"."LastEdited", "WareItem"."Article", "WareItem"."Name", "WareItem"."Description", "WareItem"."Colour", "WareItem"."Size", "WareItem"."DateIn", "WareItem"."BrandNew", "WareItem"."Sale", "WareItem"."SortOrder", "WareItem"."WareBrandID", "WareItem"."PhotoID", "WareItem"."ID", CASE WHEN "WareItem"."ClassName" IS NOT NULL THEN "WareItem"."ClassName" ELSE 'WareItem' END AS "RecordClassName", "WarePage_WareItems"."SortOrder" FROM "WareItem" INNER JOIN "WarePage_WareItems" ON "WarePage_WareItems"."WareItemID" = "WareItem"."ID" WHERE ("WarePage_WareItems"."WarePageID" = 5) GROUP BY "WareItem"."ClassName", "WareItem"."Created", "WareItem"."LastEdited", "WareItem"."Article", "WareItem"."Name", "WareItem"."Description", "WareItem"."Colour", "WareItem"."Size", "WareItem"."DateIn", "WareItem"."BrandNew", "WareItem"."Sale", "WareItem"."SortOrder", "WareItem"."WareBrandID", "WareItem"."PhotoID", "WareItem"."ID", CASE WHEN "WareItem"."ClassName" IS NOT NULL THEN "WareItem"."ClassName" ELSE 'WareItem' END, "WarePage_WareItems"."SortOrder") as all_distinct Duplicate column name 'SortOrder'

And $doSet = $this->getManyManyComponents('WareItems', '',''); works fine.

Actually I don't know why WareItem.SortOrder created by dev/?flush=1