Jump to:

22972 Posts in 11596 Topics by 2824 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, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1142 Views
  • Raccoonwao
    Avatar
    Community Member
    4 Posts

    A Page has_many another page does not show in template Link to this post

    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 SpeicalPage

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

    Any help is greatly appreciated.

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: A Page has_many another page does not show in template Link to this post

    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;   
    }

  • Raccoonwao
    Avatar
    Community Member
    4 Posts

    Re: A Page has_many another page does not show in template Link to this post

    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();
          }      
       }

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