Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Passing an ID to a URL

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

Page: 1
Go to End
Author Topic: 2708 Views
  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Passing an ID to a URL Link to this post

    I have an EventPage (page) and Event (dataobject). I want to be able to render the dataobject on a page by passing the id to the url. So for example: mysite.com/nu-events/evt/3 would be the 3rd event. I think I have everything setup correctly, but I'm getting the following error... Object::__call() Method 'getViewer' not found in class 'Event'

    I have this function in EventPage

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

    And I have a template named: EventPage_evt.ss following the ClassName_action format, but I can't get that template to show. I can get the dataobject to show if i use renderWith() ie...

    function event() {
          if($id = Director::urlParam('ID')) { // check to see we have been passed an ID
             $evt = DataObject::get_by_id("Event", $id);
             return $evt->renderWith('EventPage_evt');
          }
       }

    But that strips out the Page template stuff. How can I get SS to recognize the appropriate template. Thanks in advance for your help.

  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Re: Passing an ID to a URL Link to this post

    Got it to work with the help of UncleCheese, using this code

    function evt() {
          if($id = Director::urlParam('ID')) { // check to see we have been passed an ID
             $evt = DataObject::get_by_id("Event", $id);
             return $this->customise(array('Event' => $evt))->renderWith(array('EventPage', 'Page'));
          }
       }


    Then using EventPage.ss as my template. Calling variables $Event.name. I hope this can help someone else

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Passing an ID to a URL Link to this post

    Okay, i'm almost pulling my hair out. I get the most of Silverstripe ( i like to think) but i just can't figure this out.

    I'm using modeladmin to manage my tv-series, Serie is a DataObject and Episode is a DataObject
    I have a SeriePage.php and i have a SeriePage.ss, that works great. It shows al my Series with Covers and Discription, No problem there.

    I need to create a Link (function) to link to a (not really existing) Episodes page. (Template i guess).

    Now i tried your solution but it just won't work.

    -----------------------------------------------------------------------------------------------------------------------------
    class SeriePage_Controller extends Page_Controller {
       
    function GetEpisode() {
    if($id = Director::urlParam('ID'))
    $episode = DataObject::get_by_id("episode", $id);
    return $this->customise(array('episode' => $episode))->renderWith(array('EpisodePage', 'Page'));
    }
    }
    -------------------------------------------------------------------------------------------------------
    Then in my SeriePage.ss i use the GetEpisode function as Link.

    -----------------------------------------------
    <a href="$GetEpisode">

    -----------------------------------

    I've made a EpisodePage template

    ------------------------------------------------------------------------

    <link href="../../css/typography.css" rel="stylesheet" type="text/css" />
    <div class="typography">
       <ul>
    <% control Episode %>

    <li><a href="mms:\\\" class="left">$Episode.Title</a></li>
    <% end_control %>
    </ul>
    </div>

    ----------------------------------------------------------------------

    Where do i go wrong ?????

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Passing an ID to a URL Link to this post

    $GetEpisode is not a string, it is a DataObject. Thus, to get a link from it one would have to do $GetEpisode.Link

    However, the link for the series page should be:

    <% control Children %><!-- controlling the episodes of the series -->
    ...
    <a href="{$Series.Link}/GetEpisode/$ID">..</a>
    ...
    <% end_control %>


    Then that link will then open a page that is rendered with the EpisodePage template.

    The template then needs to control 'episode' NOT 'Episode'.

    NB: When fetching classes, the class name parameter is case sensitive

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Passing an ID to a URL Link to this post

    Pigeon (tx again ) the way you bring it, it must be so simple
    But as i mentioned...I just don't get it

    To be honest, i don't even really understand what i'm doing in my 'GetEpisode' function.
    Now the solution seems to be that i have to control the children ( i understand that episodes are - in theory - children of a serie).
    But i came across different solutions, like http://www.silverstripe.org/data-model-questions/show/257660#post257660 but then i still have nothing to put in my href...

    How is it possible that i just can't find a simple solution on how to create, in fact, just a dynamic page. I worked tru the Silverstripe book (where they seem to use it for the Jobs DataObject ), i spend days scrolling tru the forum. I think it has something to do with overloading the $Link, but i'm soooooo lost.

    All i want;

    I list my series which are DataObjects ( got that part )
    When i click on a serie it needs to open a page with the Episodes (DataObject) belonging to that serie.

    ------
    <ul>
    <li><a href= "Episodes belonging to Serie.Name">Serie.Name</a></li>
    </ul

    I really believe that i'm the only one who can't figure this out...please have patiance with me.

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Passing an ID to a URL Link to this post

    I think that posting my code would help.(?)

    ----------------------------------------------------------------------------------------------------------------------
    class Serie extends DataObject {   
    static $db = array(
    'Name' => 'Varchar',
        'Server' => "Enum('Tcamts051,Tcamts052')",
        'Folder' => "Enum('series 1,series 2, series3, series 4')",
        'Omschrijving' => 'Text'
       
    );
        static $has_one = array (
                'SerieCover' => 'Image',
                'SeriePagina' => 'SeriePagina'
                );
        static $has_many = array(
                'Episodes' => 'Episode'
                );
       static $searchable_fields = array ('Name');
       static $singular_name = 'Serie';
       static $plural_name = 'Series';
    }
    ----------------------------------------------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------------------------------------------
    class Episode extends DataObject {

       static $db = array(
    'Name' => 'Varchar',
    'Season' => "Enum('Season 01,Season 02,Season 03,Season 04,Season 05,Season 06,Season 07,Season 08,Season 09,Season 10,Specials')"
    );

    static $has_one = array(
    'MySerie' => 'Serie'
       );
          
    }
    -------------------------------------------------------------------------------------------------------------------------------------------------

    I manage those with Modeladmin

    -------------------------------------------------------------------------------------------------------------------------------------------------
    class SerieAdmin extends ModelAdmin {

       static $managed_models = array(
          'Serie',
          'Episode',
       );
       
       static $url_segment = 'series';
    }
    -------------------------------------------------------------------------------------------------------------------------------------------------

    Then for my SeriePage..
    -------------------------------------------------------------------------------------------------------------------------------------------------
    class SeriePagina extends Page {
       
       static $db = array ();
       
       static $has_many = array(
          'Series' => 'Serie'
       );
       
       static $singular_name = 'Serie Pagina';
       static $plural_name = 'Serie Paginas';

       
    }

    class SeriePagina_Controller extends Page_Controller {
       

    function GetEpisode() {
    if($id = Director::urlParam('ID')) {
    $afl = DataObject::get_by_id('Episode', $id);
    return $this->customise(array('Episode' => $afl))->renderWith(array('EpisodePage', 'Page'));
          }
       }
    }
    -----------------------------------------------------------------------------------------------------------------------------

    And my Template for listing the Series

    -----------------------------------------------------------------------------------------------------------------------------
    <div class="typography">

          <% if Series %>
             <% control Series %>
       
    <table width="757" border="1" align="center">
    <tr>
    <td width="86"><a href="AND THIS IS THE PROBLEM">$Name</a></td>
    <td width="522">&nbsp;</td>
    </tr>
    <tr>
    <td height="160">$SerieCover.Setwidth(85)</a></td>
    <td>$Omschrijving</td>
    </tr>

    </table>
             <% end_control %>
             
    <% else %>
    Geen Series
          <% end_if %>
          $Content
    </div>
    ------------------------------------------------------------------------------------------------------------------------------------------
    I did make a EpisodePage.ss
    ------------------------------------------------------------------------------------------------------------------------------------------
    <link href="../../css/typography.css" rel="stylesheet" type="text/css" />
    <div class="typography">
       <h2>$Name</h2>
                   <% if Episode %>
    <ul>
       <% control Episode %>
                      <li><a href="mms:\\\$MySerie.Server\$MySerie.Folder\$MySerie.Name\$Season\{$Name}.wmv" class="left">$Name</a></li>
                      <% end_control %>
                      </ul>
    <% else %>
    No episodes yet
    <% end_if %>
       

    </div>
    ------------------------------------------------------------------------------------------------------------------------------------

    Btw Pigeon, i hope you don't mind but i asked Hamish to look at this also 'cause he handled the same kind of question a while ago.

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Passing an ID to a URL Link to this post

    Okay, i did get a little further...

    Now i got it to render with my Episodepage.

    I had to use <a href="Serie/GetEpisode/$ID"> in my SeriePage so now i get http:\\localhost\serie\GetEpisode\1 (for the first listed serie) and \2 for the second listed serie etc.

    but now ;
    when i click on serie 1 i get the episode with ID 1
    when i click on serie 2 i get the episode with ID 2

    Somewhere in the function i need to compare the ID to the Episodes.MySerieID.

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: Passing an ID to a URL Link to this post

    Okay...changed the '::get_by_id' into '::get' now at least it shows every episode. But it shows every episode no mather what serie i choose.

    I tried to play around with the $this, but i'm missing something.

    -----------------------------------------------------------------------------------------------------------
    function GetEpisode() {
    if($id = Director::urlParam('ID')) {
    $afl = DataObject::get('Episode',$this->ID='MySerieID');
    return $this->customise(array('Episode' => $afl))->renderWith(array('EpisodePage', 'Page'));
          }
       }

    -------------------------------------------------------------------------------------------------------------------

    Any ideas?

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