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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

DataObject PAge


Go to End


15 Posts   5658 Views

Avatar
erwanpia

Community Member, 63 Posts

18 February 2009 at 11:47pm

Hi, I have read and understood the dataobject tutorials and how to display them on a page

what I would like now is to create a unique page for each dataobject row. Is this possible and how ?

Thanks

E.

Avatar
Double-A-Ron

Community Member, 607 Posts

24 February 2009 at 11:35am

Hiya,

I don't quite follow what you're trying to do here. Can you be a bit mroe detailed?

Cheers
Aaron

Avatar
erwanpia

Community Member, 63 Posts

25 February 2009 at 6:41am

Edited: 25/02/2009 6:58am

Ron, I was enquiring about dataobjects : I know how to display a list of records on a page, but I don't know how to create a unique url for each record of a databobject

Avatar
UncleCheese

Forum Moderator, 4102 Posts

25 February 2009 at 8:08am

If the DataObject needs a URLSegment, it needs to be a Page, not a DataObject..

I think you're misunderstanding something.. ?

Avatar
Willr

Forum Moderator, 5523 Posts

25 February 2009 at 3:53pm

but I don't know how to create a unique url for each record of a databobject

The forum module does something like this, You need to have 'Holder' page though. Eg a forum thread isnt a page in the site but by using the ID of the thread in the url and a function 'show' or something you can show your dataobject.

So if its not a page object you can do something like this in your controller class (mysite/code/page.php). It could be called anything

function view() {
if($id = Director::urlParam('ID')) { // check to see we have been passed an ID
return DataObject::get_by_id("SomeDataObject", $id);
}
}

In the template you could do $view.Title or $view.Date to access the information in the DataObject.

Then you would have the url like yoursite.com/home/view/12 to view the 12th dataobject. Thats just a basic example which is easily expanded (eg if that object doesn't exist)

Avatar
theAlien

Community Member, 131 Posts

15 July 2009 at 7:50am

Hi, this is exactly what I was looking for, however I can't get it working.
First I did what willr wrote in his post and added this to PersonsPage.php:

class PersonsPage_Controller extends Page_Controller {
	function view(){
		if($id = Director::urlParam('ID')){
		return DataObject::get_by_id("PersonsObject", $id);
		}
	}
}

That returned a "Page not found" and no signs of my PersonsPage.ss-template.

So I did some research in the forum-module, and added the following lines to PersonsObject.php:

function Link() {
	return "show/" . $this->ID;
}

Right now I can call $Link in PersonsPage.ss instead of fx a hardcoded localhost/myss/show/1
That's nice, but I still get no signs of my PersonsPage.ss-template (and still the same "Page not found"-message.

Does someone have any suggestions?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

15 July 2009 at 8:02am

You need a url segment first so SS knows which controller to use.

www.mysite.com/the-persons-page-you-want-to-view/view/123

Avatar
theAlien

Community Member, 131 Posts

16 July 2009 at 12:53am

Edited: 16/07/2009 1:02am

yeah, that makes sense.

But now I stumble upon two other learning-opportunities (problems don't exist, do they?):

1: how do I retrieve the URLSegment dynamically?

adding $this->PersonsPages()->URLSegment or $this->URLSegment to the function Link() rendered http://www.mysite.com/view/123.

In the forummodule the following string is used: $this->Forum()->Link(), however that gives an error: Uncaught Exception: Object->__call(): the method 'link' does not exist on 'ComponentSet'. Because the error didn't occur on the forummodule, I searched in the module for another place where a method Link() is defined, but couldn't find it.

2: hardcoding the urlsegment (http://www.mysite.com/the-persons-page-you-want-to-view/view/123) renders a white screen (nothing on it and no firebug-errors). I checked the templates and there is a PersonsObject.ss template with content in the mysite/template/Layout-folder so that shouldn't be the cause (I also tried moving it to mysite/template or renaming it to PersonsPage-view.ss (as is done in the forum-module) but that didn't work either)

BTW, I found some errors in my original code, so I'll post a better version here:

class PersonsPage_Controller extends Page_Controller {
		function view($id = null) {
		if($id == null)
			$id = $this->urlParams['ID'];
		if($id && is_numeric($id))
			return DataObject::get_by_id("PersonsObject", $id);
			
	}

Go to Top