Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Accessing Product properties from a Sitetree object

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

Page: 1
Go to End
Author Topic: 1266 Views
  • chris_d
    Avatar
    Community Member
    21 Posts

    Accessing Product properties from a Sitetree object Link to this post

    Edit:
    Turns out this was embarissinbly simple... DataObject::get_by_id( 'Product', $data->ID) was what i was looking for

    old post:
    Hey guys,

    Great CMS btw, very extendable

    My question is say I've got a Product page from Sitetree as an object, how do I access its product properties?

    Ie I've got $data which is a product page, so if I type echo $data->Title; I get the title of the product
    How do I print the price of the product? I can't just do echo $data->Price as it's accessing the Sitetree table not the Product table.
    Do I need to perform a SQL query to search for a product in product table with the same ID as the page?

    Any hints would be great,

    Thanks
    chris

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Accessing Product properties from a Sitetree object Link to this post

    Hi Wombleme

    You can access the product page properties in various ways, but essentially you grab the page from the database and then access it as a DataObject.

    In a template you use control blocks (<% control SomeFunction %>) to return sets of DataObjects that you can then access recursively.

    Lets say you have a function in your model which returns all of your product pages, it might look something like this:

    function GetProducts(){
    return DataObject::get("ProductPage");
    }

    then in your template you can do this:

    <% control GetProducts %>
    $Title
    $InStock
    $Price
    $Description
    <% end_control %>

    There are other ways, for example <% control Children %> will return all of the children of the current page in the same way as above.

    Alternatively if you are only returning a single object then you don't need control blocks at all, you can just do something like $GetProduct.Price

    Hope that helps

  • chris_d
    Avatar
    Community Member
    21 Posts

    Re: Accessing Product properties from a Sitetree object Link to this post

    Thanks aram, very useful like most things in ss it seems alot simpler after examples

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