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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

A Page has_many another page does not show in template

Go to End

3 Posts   1934 Views


Community Member, 4 Posts

27 March 2011 at 10:50pm


This is a question involving one of the classes under ecommerce module but I think it is more like a general question about customized page relationship. So I believe better to put under General Questions.

I have created an additional relation for a PageA class to PageB class by:

class SpecialPage extend Page {

static $has_many = array(
    'MyProducts' => 'Product'



where Product is the ecommerce Product class which extends Page as well.

After successfully database rebuilt and put some testing relationships, database data is updated correctly, i.e. table Product gets a new column called SpecialPageID with the corresponding page IDs. However, this is not able to be displayed in the template:

<% if MyProducts %>
<% control MyProducts %>

$ProductName, $Price... 
($Pos / $TotalITems)

<% end_control %>
<% else %>
no products...
<% end_if %>

No matter how many Products are linked to a SpecialPage, it always shows "no products..."

It works fine if:

1. the SpecialPage has_many non-page object, or
2. the Product (which is a page) is a child page of the SpeicalPage

Am I missing something for Page-has_many-page relationship?

Any help is greatly appreciated.


Forum Moderator, 1199 Posts

28 March 2011 at 2:56am

I've tested a similar setup where Product has_one Page and Page has_many product, and that works - providing the Product is 'Published' and has, if translatable is enabled, the same locale. So this is at least possible in certain conditions :-)

You could check by doing a ?showqueries=1 on your frontend (must be logged in and in 'dev' mode) and see what query SilverStripe comes up with.

Better yet - create a method in your SpecialPage controller that gives you a bit more control:

<% control ShowMyProducts %>
	Product name: $ProductName
<% end_control %>

public function ShowMyProducts() {
	$myProducts = $this->MyProducts();

	// perform some check, check $myProducts->Count() or
	// echo something to see if the function gets called at all
	// make it easy to 'spot' the correct query when ?showqueries=1

	return $myProducts;	


Community Member, 4 Posts

28 March 2011 at 5:48am

Edited: 28/03/2011 5:48am

Right, now I have overrided onAfterSave() to publish ALL products (because I don't know which products are involved). It is slow but works. Thanks alot!

function onAfterWrite() {

$products = DataObject::get('Product');

foreach ($products as $product) {