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   8854 Views

Avatar
181920

Community Member, 8 Posts

5 July 2010 at 11:10am

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

Avatar
181920

Community Member, 8 Posts

5 July 2010 at 2:40pm

Edited: 05/07/2010 2:40pm

-

Avatar
Graphicator

Community Member, 62 Posts

19 July 2010 at 2:36am

Edited: 19/07/2010 2:38am

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.

Avatar
Graphicator

Community Member, 62 Posts

19 July 2010 at 3:05pm

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.

Avatar
stew

Community Member, 30 Posts

26 July 2010 at 9:51pm

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

?>

Avatar
Enclave SSC

Community Member, 31 Posts

26 July 2010 at 9:58pm

Edited: 26/07/2010 9:59pm

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 %>

Avatar
stew

Community Member, 30 Posts

26 July 2010 at 10:08pm

Thanks Enclave! That code worked perfectly =)

Avatar
Matt Howard

Community Member, 3 Posts

30 September 2010 at 4:19pm

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

Go to Top