Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Querying edited content in custom function.

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

Page: 1
Go to End
Author Topic: 490 Views
  • Hibari
    Community Member
    9 Posts

    Querying edited content in custom function. Link to this post

    If anybody could help, it'd be much appreciated. I'm trying to query for content that would be edited in the CMS of Silverstripe, but not within my page.ss I want the content that is edited to display in accordance with a day function code that I wrote. That way someone can edit the content specifically for every monday weekly, tuesday, wednesday- and so forth. I'm currently having trouble cause I'm not sure what to call. But, this is my code for it.

    For example:

    * Defines the HomePage page type

    class HomePage extends Page {
    static $db = array(
    'HSun' => 'HTMLText',
    'HMon' => 'HTMLText'

    static $has_one =array(


    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root', new TabSet('Daily'));
    $fields->addFieldToTab('Root.Daily.Sunday', new HTMLEditorField('HSun', 'Sunday Service'));
    $fields->addFieldToTab('Root.Daily.Monday', new HTMlEditorField('HMon', 'Monday Service'));
    return $fields;

    class HomePage_Controller extends Page_Controller {

    function days() {

    $sunday = '0';
    $monday = '1';
    $tueday = '2';
    $wednesday = '3';
    $thursday = '4';
    $friday = '5';
    $saturday = '6';

    $today = date("w");

    $CT = "Current Time: ".date("G:i");

    $wed = DB::get($HSun);

    //return "$today";

    return $wed;


    If anybody knows how to query it correctly, that'd be great, because that doesn't work at all, and I can't seem to figure it out...

  • Willr
    Forum Moderator
    5502 Posts

    Re: Querying edited content in custom function. Link to this post

    $wed = DB::get($HSun);

    You'd be getting an error on that, HSun isn't defined as a variable.

    If I understand correctly, you want separate HTMLText fields for each day of the week? and in that case I'd do something like

    switch($today) {
    case '0':
    return $this->dbObject('HSun');
    case '1':
    return $this->dbObject('HMon');

    $this->dbObject('Foo') returns the Database field object, were as $this->Foo will return just the value inside that field. Usually the object is more useful to have.

  • Hibari
    Community Member
    9 Posts

    Re: Querying edited content in custom function. Link to this post

    Hey Willr,
    thank you so much. That was the perfect thing I needed to know.
    Secondly, thanks for the switch technique, my original one was having trouble with the days so I decided to use the switch technique and it's all functioning perfectly now. Thank you very much.

    Out of curiosity, is it hard to add an $has_one piece into that return as well? I was considering using the $has_one to allow image uploading to a ImageField in silverstripe. Would I produce that the same way, or would the code be different?

    Once more, thanks for all your help.


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.