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.

Archive /

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

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

Querying Database via a Class function from _config.php


Reply


4 Posts   1470 Views

Avatar
spenniec

Community Member, 37 Posts

15 May 2008 at 9:34pm

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.

Avatar
dio5

Community Member, 501 Posts

16 May 2008 at 9:41pm

Edited: 16/05/2008 9:44pm

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.

Avatar
spenniec

Community Member, 37 Posts

17 May 2008 at 8:54am

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?

Avatar
mage

Community Member, 10 Posts

26 May 2008 at 9:17pm

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