Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Multiple referenced items/pages

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

Page: 1
Go to End
Author Topic: 1001 Views
  • neros3
    Avatar
    Community Member
    51 Posts

    Multiple referenced items/pages Link to this post

    Hi

    Im trying to create a front page (the first page, also called HomePage) that contains a selection of teasers (other pages).

    Im currently able to select multiple pages from in the CMS, my problem is now how to retrieve these related pages on my frontpage.

    I have tried with:

    <% control ReferencedPages %>
    Title: {$Title}
    <% end_control %>
    - but this doesnt work.

    I have also tried in the controller:

    $this->ReferencedPages()
    ..
    and
    ...
    foreach($this->ReferencedPages() as $item){
    Debug::show($item);
    }
    - but also here no luck!

    So right now Im pretty much all out of ideas.

    I have pasted the models (excerpt) below.

    Thanks!

    class HomePage extends Page {

    public static $has_many = array(
    "ReferencedPages" => 'SiteTree'
    );
    ...
    $fields->addFieldToTab("Root.Content.Main", new TreeMultiselectField("ReferencedPages", "Choose page to show", "SiteTree"));

    class Page extends SiteTree {

       public static $has_one = array(
    'HomePage' => 'Page'
       );
    ...

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: Multiple referenced items/pages Link to this post

    With a has_many relationship it should have created a column on the HomePage table called "HomePageID" check if this is set in your database. If it is a null value then $this->ReferencedPages won't show anything as the HomePageID is not set.

    http://doc.silverstripe.org/treemultiselectfield mentions that its designed for managing many_many relations rather than many to one so you could be running into issues because of that.

  • neros3
    Avatar
    Community Member
    51 Posts

    Re: Multiple referenced items/pages Link to this post

    Hi

    Thanks for your reply!

    Is there another field for has_many relationships I should use insted?
    Or should I just create it as a many_many and then just use is as a has_many? My thoughts were just that a has_many is simpler than a many_many.

    I have tried with a has_one, and that worked out for me.

    Thanks

  • neros3
    Avatar
    Community Member
    51 Posts

    Re: Multiple referenced items/pages Link to this post

    Hi Willr

    The HomePageID is created on the Page table and not the HomePage table. But I suppose this is correct.
    The HomePageID is set (for the ones that I have chosen via the interface) correctly.
    So everything looks ok.

    But I still can't get anything out in my template or in the controller code.

    <% if ReferencedPages %>
    testing...
    <% end_if %>
    ... doesnt work

    Debug::show($this->ReferencedPages());
    shows:
    ComponentSet   
    Type: 1-to-many
    Size: 0

    Have anyone tried this before?

    Thanks!

  • neros3
    Avatar
    Community Member
    51 Posts

    Re: Multiple referenced items/pages Link to this post

    Can anyone confirm that the HomePageID is created on the Page table and not the HomePage table. So I at least know that the data is correct?

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: Multiple referenced items/pages Link to this post

    Sorry neros3, that was a typo in my orignal post. Yes it should be on the Page table.

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