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.

All other Modules /

Discuss all other Modules here.

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

DataObjectManager to enhance Blog module?


Go to End
Reply


7 Posts   2333 Views

Avatar
PeterB

Community Member, 18 Posts

18 August 2009 at 3:55am

I've been using the Blog module and it does a good job. However, after a few months of daily blogging, having each blog entry in the site tree is painful, and using a datagrid to manage them in the admin area would be much easier.

If I understand correctly, there's no way to give an item a URL unless it extends Page - because nothing has a URL alias assigned unless it's present in the site tree. Neither is there any option to say 'This is a page, but hide it in the admin site tree'.

Because of this, my attempts so far to use DataObjectManager (along the lines of its testimonial example) have failed.

Has anyone else managed, or got ideas for how this can work?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 August 2009 at 4:39am

You can absolutely give a DataObject subclass its own page.

pseudo code..

MyDataObjectHolder extends Page
{
has_many MyDataObjects...

function view()
{
return DataObject::get_by_id("MyDataObject",$this->urlParams['ID']);
}
}

then in MyDataObjectHolder.ss

/dataobject-holder/view/123
<% control view %>
$DataObjectProperty1
$DataObjectProperty2
<% end_control %>

Avatar
PeterB

Community Member, 18 Posts

18 August 2009 at 9:01pm

Thanks UncleCheese, I'm getting somewhere however run into an error.

My code:

<?php
class PrayerPage extends Page
{
  static $has_many = array (
    'Prayers' => 'Prayer'
  );

  public function getCMSFields()
  {
    $f = parent::getCMSFields();
    $f->addFieldToTab("Root.Content.Prayers", new DataObjectManager(
      $this,
      'Prayers',
      'Prayer',
      array('Date' => 'Date','Title'=>'Title','Author' => 'Author'),
      'getCMSFields_forPopup'
    ));
    return $f;
  }

}

class PrayerPage_Controller extends Page_Controller {
  public function view() {
    return DataObject::get_one('Prayer', "Date='{$this->urlParams['ID']}'");
  }

}
?>

Stepping through with the debugger, the right record is fetched from the database. However things go haywire and die with the error:

[User Error] Uncaught Exception: Object->__call(): the method 'getviewer' does not exist on 'Prayer'
GET /pray-for-us/view/2009-08-12

Line 551 in /var/www/navs/htdocs/sapphire/core/Object.php

Any idea what causes this error?

Thanks!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

19 August 2009 at 1:29am

Template?

Avatar
PeterB

Community Member, 18 Posts

19 August 2009 at 2:03am

Edited: 19/08/2009 2:04am

PrayerPage.ss:

<div id="feature"></div>

<div id="maincol">

<% control view %>
<p>$Date
<p>$Title
<p>$Content
<% end_control %>



<h1>$Title</h1>
  <% if Level(2) %>
      <% include BreadCrumbs %>
  <% end_if %>
  $Content
  $Form
  $PageComments

</div>

<% include SideBar %>

Avatar
UncleCheese

Forum Moderator, 4102 Posts

19 August 2009 at 3:50am

try $view.Date, $view.Title, etc.. instead of jumping into the control.

Here's a good tutorial for you
http://www.silverstripe.org/data-model-questions/show/254345#post254345

Avatar
PeterB

Community Member, 18 Posts

21 August 2009 at 2:26am

To document the fix for others:

You can't return a DataObject from a controller method! I copied this code from somewhere, I guess it's a template object being returned:

class PrayerPage_Controller extends Page_Controller {
  public function view() {
    $object = DataObject::get_by_id('Prayer', $this->urlParams['ID']); #get_one('Prayer', "Date='{$this->urlParams['ID']}'");

    if($object) {
      return $this
         ->customise(array('Prayer' => $object))
         ->renderWith(array('Prayer', 'Page'));
   } else {
      Director::redirect(Director::absoluteBaseURL() . 'not-found');
   }
  }
}