Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Querying Database via a Class function from _config.php

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1372 Views
  • spenniec
    Avatar
    Community Member
    37 Posts

    Querying Database via a Class function from _config.php Link to this post

    Hi,
    I would like to shift the theme selection process to a Page in the CMS.
    I have the following;

    class Config extends Page {

       static $db = array(
          'Theme' => 'Text',
       );
       
       function get_current_theme() {
          $result = DataObject::get_one('Config');
          return $result->Theme;
       }
       
    The function returns the value of Theme if I call $get_current_theme from a template but if I call it from mysite/_config.php using

    Config::get_current_theme();

    I get the following errors, which I assume are because I am ahead of the class construction of SS.

    Warning: ClassInfo::dataClassesFor() no parents for Config in sapphire\core\ClassInfo.php on line 49

    Warning: Invalid argument supplied for foreach() in sapphire\core\ClassInfo.php on line 50

    Fatal error: DataObject::buildSQL: Can't find data classes (classes linked to tables) for Config in sapphire\core\model\DataObject.php on line 1334
    Can I achieve the same DB query using the SQLQuery function? If so how, I can't work out the syntax from the doco or api pages.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Querying Database via a Class function from _config.php Link to this post

    AFAIK you can only do

    Config::get_current_theme();

    if you made get_current_theme() a static method.

    See for example: http://theserverpages.com/php/manual/en/language.oop5.static.php

    Be aware though that static vars/methods are tied to the class rather than to an instance of it.

  • spenniec
    Avatar
    Community Member
    37 Posts

    Re: Querying Database via a Class function from _config.php Link to this post

    Dio
    Thanks for that.
    I can get the get_current_theme() code to run, my issue is querying the Config table for the value of Theme.
    At the very least what I want to do is "SELECT * FROM Config" and then get the value of Theme from the 1 row which is returned (as I only have one row..)

    But how to do it from my Config.php class function without the errors listed on original post?

  • mage
    Avatar
    Community Member
    10 Posts

    Re: Querying Database via a Class function from _config.php Link to this post

    just wondering if you go this working, cos i would like to be able to select a theme from inside the cms as well...

    if you did could you post the code?

    thanx

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