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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Accessing Product properties from a Sitetree object


Go to End


3 Posts   2978 Views

Avatar
chris_d

Community Member, 21 Posts

15 January 2009 at 6:23am

Edited: 15/01/2009 6:33am

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

Avatar
Carbon Crayon

Community Member, 598 Posts

15 January 2009 at 6:42am

Edited: 15/01/2009 6:44am

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 :)

Avatar
chris_d

Community Member, 21 Posts

20 January 2009 at 6:31am

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