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.

Data Model Questions /

Display a DataObject on a new "own" page


Go to End
Reply


24 Posts   8620 Views

Avatar
markus85

Community Member, 12 Posts

3 April 2009 at 12:07am

Hello everybody, I´m very new to silverstripe...

I want to show a DataObject in an own page how can I do this?

I created a holder-class wich contains and lists all DataObjects in the database-table - that works fine.
like here :

  • - Object-String1
  • - Object-String2
  • - Object-String3

Now I want a link on this "Object-String". A link to a new page wich displays the detailed data of my DataObject.
The DataObject are created by a form in the frontend or backend.

I think it should work like this:
open the page for the detailed DataObject --> get the ID of the DataObject to display -->get the data out of the database

How can I realize this ?

I hope you understand my issue...

Avatar
Hamish

Community Member, 712 Posts

7 April 2009 at 9:07pm

Hi,

You will need to create an action on your page controller. This action will tell silverstripe which DataObject to show and how to show it.

class ObjectDisplayPage extends Page {

}

class ObjectDisplayPage _Controller extends Page_Controller {

   function display() {
      $params = Director::urlParams();
      $id = (int)$params['ID'];
      $object = DataObject::get_by_id($id);
      if($object) {
         return $this
         ->customise(array('DisplayObject' => $object))
         ->renderWith(array('DisplayObjectPage', 'Page'));
      } else {
         Director::redirect('not-found');
      }
   }

}

Create a new "Object Display Page". Now when you navigate to the page url, you can add /display/5 (for example) to the url.

"display" is the action on the URL. "5" is the ID url parameters.

The function will do the following:
1. Retrieve the dataobject
2. Test that it exists.
3. If doesn't exist, redirect the user to a 'not-found' page
4. If it DOES exist, first customise the current output so the object "DisplayObject" has your object
5. then, render the page using the template "DisplayObjectPage"

Now you jsut need to create the template 'DisplayObjectPage'. This template will have the output you want to see, eg:

<% control DisplayObject %>
$Title
$Description
...
<% end_control %>

Avatar
Stefdv

Community Member, 110 Posts

10 January 2010 at 2:59am

Hamish,

This is possibly not the right way to do it ( i don't mean to 'bypass' Pigeon) but could you please also take a look at my question in this post? http://www.silverstripe.org/data-model-questions/show/255257?start=0#post276408

I thought you used a different approach to the problem (at least...for me it's a problem ) but it seems you use the same method.

Tx already

Avatar
LinseyM

Community Member, 99 Posts

20 February 2010 at 3:41am

Hi there,

Sorry to hijack the thread but I'm trying to do the same thing: so is this the recommended way to do it? Am not too worried about friendly URLs as its only a small part of the site.

I was working through the "Job Posting" part of the book as I was trying to do something similar, but then it just lost me!!!

:)

Thanks,

Avatar
edk

Community Member, 39 Posts

20 February 2010 at 7:12am

Edited: 20/02/2010 7:13am

Hamish is spot on for the down and dirty approach here (by that I mean - not seo friendly in the URL)... For additional details on this method check out the two articles on SSBits:

1. [url]http://www.ssbits.com/using-silverstripe-url-parameters-to-display-dataobjects-on-a-page/[/url]
2. [url]http://www.ssbits.com/display-dataobjects-in-an-seo-friendly-way/[/url]

Article 2 extends the steps in the first article to turn it into a more SEO friendly URL.

If you haven't already fire up your RSS reader for SSBits [url]http://www.ssbits.com/[/url]...it is a great resource for tips etc on Silverstripe.

Avatar
LinseyM

Community Member, 99 Posts

2 March 2010 at 5:57am

Hi there,

Thanks for those article links - the first one was really helpful... was struggling to get this to work without understanding what the code was doing. Got it now.

Much appreciated.

:)

Avatar
timwjohn

Community Member, 98 Posts

30 March 2010 at 12:12am

Does the nested URL support in 2.4 allow for multiple parameters to be passed?

For example, I want to display all items in a given list of categories, would I be able to set something up like:

/itemsbycategory/2/6/15

I understand that this doesn't reflect the true sense of nested URLs, but is it still possible? It's also not necessary (in this project) to have friendly URL, so the ID method is fine by me.

Avatar
timwjohn

Community Member, 98 Posts

31 March 2010 at 11:33am

I guess that you can't have more than 2 parameters as URL segments then. Only 'action' and 'id'. I admit I haven't fully investigated yet, so I'll post back if I find out otherwise.

One thing that I got stuck on for anyone else going for Hamish's method - don't forget to add the name of the function to the $allowed_actions array in your page controller class.

Go to Top