Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Linking list of Teasers (TreeMultiselectField)

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

Page: 1
Go to End
Author Topic: 1582 Views
  • maxx
    Avatar
    Community Member
    4 Posts

    Linking list of Teasers (TreeMultiselectField) Link to this post

    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

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: Linking list of Teasers (TreeMultiselectField) Link to this post

    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');

  • Tonyair
    Avatar
    Community Member
    81 Posts

    Re: Linking list of Teasers (TreeMultiselectField) Link to this post

    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

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