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.

Data Model Questions /

Multiple referenced items/pages


Reply


6 Posts   1032 Views

Avatar
neros3

Community Member, 51 Posts

26 July 2010 at 10:37am

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

Avatar
Willr

Forum Moderator, 5513 Posts

26 July 2010 at 11:23am

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.

Avatar
neros3

Community Member, 51 Posts

26 July 2010 at 7:56pm

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

Avatar
neros3

Community Member, 51 Posts

27 July 2010 at 4:35am

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!

Avatar
neros3

Community Member, 51 Posts

29 July 2010 at 2:15am

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

Avatar
Willr

Forum Moderator, 5513 Posts

29 July 2010 at 11:10am

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