Jump to:

10928 Posts in 2612 Topics by 1809 members

All other Modules

SilverStripe Forums » All other Modules » DataObjectManager to enhance Blog module?

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 2020 Views
  • PeterB
    Avatar
    Community Member
    18 Posts

    DataObjectManager to enhance Blog module? Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager to enhance Blog module? Link to this post

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

  • PeterB
    Avatar
    Community Member
    18 Posts

    Re: DataObjectManager to enhance Blog module? Link to this post

    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!

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager to enhance Blog module? Link to this post

    Template?

  • PeterB
    Avatar
    Community Member
    18 Posts

    Re: DataObjectManager to enhance Blog module? Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObjectManager to enhance Blog module? Link to this post

    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

  • PeterB
    Avatar
    Community Member
    18 Posts

    Re: DataObjectManager to enhance Blog module? Link to this post

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

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