21309 Posts in 5738 Topics by 2603 members
General Questions
SilverStripe Forums » General Questions » A Page has_many another page does not show in template
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 946 Views |
-
A Page has_many another page does not show in template

27 March 2011 at 10:50pm
Hi,
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:
SpecialPage.ss
<% 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 SpeicalPageAm I missing something for Page-has_many-page relationship?
Any help is greatly appreciated.
-
Re: A Page has_many another page does not show in template

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=1return $myProducts;
} -
Re: A Page has_many another page does not show in template

28 March 2011 at 5:48am Last edited: 28 March 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() {
parent::onAfterWrite();
$products = DataObject::get('Product');foreach ($products as $product) {
$product->doPublish();
}
}
| 946 Views | ||
|
Page:
1
|
Go to Top |
