Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject PAge

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

Page: 1 2
Go to End
Author Topic: 4722 Views
  • erwanpia
    Avatar
    Community Member
    63 Posts

    DataObject PAge Link to this post

    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.

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: DataObject PAge Link to this post

    Hiya,

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

    Cheers
    Aaron

  • erwanpia
    Avatar
    Community Member
    63 Posts

    Re: DataObject PAge Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObject PAge Link to this post

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

    I think you're misunderstanding something.. ?

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: DataObject PAge Link to this post

    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)

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: DataObject PAge Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObject PAge Link to this post

    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

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: DataObject PAge Link to this post

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

    4722 Views
Page: 1 2
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.