Jump to:

3461 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Display a DataObject on a new "own" page

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

Page: 1 2 3
Go to End
Author Topic: 8461 Views
  • 181920
    Avatar
    Community Member
    8 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Hi Enclave, are you able to post your code for this? i seem to be having a few issues it.

  • 181920
    Avatar
    Community Member
    8 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    -

  • Graphicator
    Avatar
    Community Member
    62 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    So I tried adding Director::addRules in _config

    Director::addRules(100, array(
    '$Controller/$Action/$ID/$OtherID/$NextID/$FurtherID/$AnotherID/$YetAnotherID' => 'MyPage_Controller'
    ));

    And then some allowed actions in MyPage_Controller class

    class MyPage_Controller extends Page_Controller {
       static $allowed_actions = array(
    'OtherID','NextID'
    );

    And then

    if ($this->request->latestParam('Action')) {
    $parameter = $this->request->latestParam('OtherID');
    if ($parameter) {
    $DataObjectID = Convert::raw2xml($parameter);
    if($DataObjectID){
    $DataObjectInfo = DataObject::get_one('CustomClass', "Title = '{$DataObjectID}'");
    return array($this, "CustomClass"=>$DataObjectInfo);
    }
    }
    }

    And the template shows up but with a blank for the $OtherID

    $Action/$ID/$OtherID


    The Action and ID shows up.

    All I'm trying to do is create a DataObject on it's own url, and looked around but not finding anything that recognizes the url($OtherID) as the DataObject's title column.

  • Graphicator
    Avatar
    Community Member
    62 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Well I reread the SSBits, SEO-friendly article. I was really glad to read the comments below for the SilverStripe 2.4 version. I got this to work finally after a few hours. I just took a 7 page site to 70+ pages, so for that purpose I'm really happy to get this working.

  • stew
    Avatar
    Community Member
    30 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    I'm stuck with the exact same problem in 2.4

    I've got a lot of Organisations managed using ModelAdmin and can't for the life of me get it working to show them on the public side of the site with their own pages - I have the search bit working reasonably well but need to give each dataobject it's own "page", has anyone managed to get this working?

    Part of the problem I need to resolve is the pulling of data from the has_many and many_many sections of the dataobject as well, the code for the dataobject I'm dealing with is below (I've removed the CMS field functions):

    Organisation.php

    <?php

    class Organisation extends DataObject {

       static $db = array(
          'Name' => 'Varchar',
          'HouseNameNumber' => 'Varchar',
          'Address1' => 'Varchar',
          'Address2' => 'Varchar',
          'Address3' => 'Varchar',
          'PostCode' => 'Varchar',
          'Description' => 'Text',
          'Phone' => 'Varchar',
          'Email' => 'Varchar',
          'Website' => 'Varchar',
          'Latitude' => 'Varchar',
          'Longitude' => 'Varchar',
          'LegalStatusNumber' => 'Varchar',
          'ParentOrganisation' => 'Varchar',
          'JoiningIntstructions' => 'Text',
          'SearchTerm' => 'Text',
          'Public' => 'Boolean'
       );

       static $has_many = array(
          'Contacts' => 'Contact',
          'MeetingTimes' => 'MeetingTime'
       );

       static $has_one = array(
          'Category' => 'Category',
          'LegalStatus' => 'LegalStatus'
       );
       
       static $many_many = array(
          'TargetGroups' => 'TargetGroup',
          'GeographicalAreas' => 'GeographicalArea',
          'Venues' => 'Venue'
       );
       
       static $searchable_fields = array(
          'Name' => 'PartialMatchFilter',
          'Address1' => 'PartialMatchFilter',
          'Address2' => 'PartialMatchFilter',
          'Address3' => 'PartialMatchFilter',
          'PostCode' => 'PartialMatchFilter',
          //'Category' => 'PartialMatchFilter'
       );
       
    }

    ?>

  • Enclave SSC
    Avatar
    Community Member
    31 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Hi Stew

    Create a holder type for the page to display the dataobjects. Then place something similar to below into the holders controller.

    class OrganisationHolder_Controller extends Page_Controller {

       static $allowed_actions = array(
       'retrieve_org'
       );

       public function getIndividualOrganisation(){
          if ($this->request->latestParam('Action')) {
             $parameter = $this->request->latestParam('ID');
             if ($parameter) {
                $OrganisationID = Convert::raw2xml($parameter);
                if($OrganisationID){
                   $OrganisationInfo = DataObject::get_by_id('Organisation', $OrganisationID);
                   return $OrganisationInfo;
                }
             }
          }
       }

    }

    Access in template like:

    {$Top.Link}retrieve_org/{$ID}

    And to display:

    <% if IndividualOrganisation %>
    <% control IndividualOrganisation %>

  • stew
    Avatar
    Community Member
    30 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    Thanks Enclave! That code worked perfectly =)

  • Matt Howard
    Avatar
    Community Member
    3 Posts

    Re: Display a DataObject on a new "own" page Link to this post

    I've been struggling with the semantic urls for displaying dataobjects on a page. I'm pasting the code I finally got to work drawing on discussions on this page and the "Display DataObjects in an SEO friendly way" article on SSBits.

    StaffMember.php

    <?php
    class StaffMember extends DataObject {
       
       static $db = array(
          'Name' => 'Varchar(255)',
          'Description' => 'Text',
          'URLSegment' => 'Varchar(255)'
       );
       static $has_one = array(
          'StaffPage' => 'StaffPage',
          'Photo' => 'Image'
       );   
       
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Name'),
             new TextareaField('Description'),
             new ImageField('Photo')
             );
       }
       
    public function getThumb(){
          if($this->PhotoID)
             return $this->Photo()->CMSThumbnail();
          else
             return '(No Photo)';
       }   
       
    /*      public function URL(){
    return $this->ID.'-'.$this->URLSegment;
    }   */

       public function onBeforeWrite(){
             if($this->Name){
                $this->URLSegment = SiteTree::GenerateURLSegment($this->Name);
                if($object = DataObject::get_one($this->ClassName, "URLSegment='".$this->URLSegment."' AND ID !=".$this->ID)){
                   $this->URLSegment = $this->URLSegment.'-'.$this->ID;
                }
             } else {
                $this->URLSegment = SiteTree::GenerateURLSegment($this->ClassName.'-'.$this->ID);
             }
             parent::onBeforeWrite();
          }

    }
    ?>

    StaffPage.php

    <?php
    class StaffPage extends Page {
       
    static $has_many = array(
       "StaffMembers" => "StaffMember",
    );
       
    function getCMSFields() {
       $fields = parent::getCMSFields();
       $dataobjectmanager = new DataObjectManager(
          $this,
          'StaffMembers',
          'StaffMember',
          array('Thumb' => 'Photo', 'Name' => 'Name', 'Description' => 'Description'),
          'getCMSFields_forPopup'
       );
       $dataobjectmanager->setPluralTitle("a member of staff");
       $fields->addFieldToTab("Root.Content.StaffMembers", $dataobjectmanager);   
       return $fields;
    }
    }

    class StaffPage_Controller extends Page_Controller {
       
       // Show works fine whether or not I allow it...
       static $allowed_actions = array(
          'show'
       );

       function show($staffMemberURL) {
          $URLSegment = Convert::raw2sql(Director::urlParam("ID"));
          $staffMember = DataObject::get_one("StaffMember", "URLSegment = '{$URLSegment}'");
          if ($staffMember) {
             $data = array(
                $this,
                "StaffMember" => $staffMember
             );
             return $this->customise($data)->renderWith(array('StaffPage', 'Page'));
          } else {
             Director::redirect('/page-not-found');
          }      
                
          return $URLSegment;
       }

    }
    ?>

    StaffPage.ss

    <div class="typography">
                
       <h2>$Title</h2>

          <% if StaffMember %>
             <% control StaffMember %>
                <ul>
                   <li>$Photo.SetWidth(100)</li>
                   <li>Name: $Name</li>
                   <li>Description: $Description</li>
                </ul>
             <% end_control %>
          <% else %>
             <% control StaffMembers %>
                <a href="{$Top.Link}show/{$URLSegment}" >$Photo.SetWidth(100)</a>
             <% end_control %>
          <% end_if %>
    </div>

    I was getting hung up on the difference between getIndividualStaffMember from the "URL Parameters" tutorial and StaffMember used in the updated snippet in the comments section of "SEO Friendly" for 2.4. Note that you have to edit the template to catch the DO rendered as a page, e.g. <% if StaffMember %> rather than <% if IndividualStaffMember %>.

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