Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » SS3 - returning datalist as array always throwing error

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

Page: 1
Go to End
Author Topic: 991 Views
  • obj63mc
    Avatar
    Community Member
    24 Posts

    SS3 - returning datalist as array always throwing error Link to this post

    Currently with SS3 I am always getting an error when trying to return a datalist as JSON and since we can't debug with isDev=1 due to a core SS bug and no error is being written to log I can't figure out why the following code is erroring out.

    $sql = "CALL get_closest_state(".$lat.",".$long.");";
             
    $results = DB::query($sql);
    for ($i = 0; $i < $results->numRecords(); $i++) {
    $record = $results->nextRecord();
       $states[] = $record['State'];   
    }
             
    $programs = Program::get()->filter(array('StateID' => $states))->toNestedArray();
    return json_encode($programs);

    Note in the above if I dump out $states it is a proper array with two strings so I know the SP is running fine. Just for some reason any time I try to query my Program dataobject it always fails. If I remove the ->toNestedArray() and view the var I see it is a datalist but when I try to get any data from that datalist it just errors out.

    Any help would be great. I have been seeing many issues with the new SS3 data model where it just does not seem to work even through I am following the api documentation.

    Thanks in advance,
    Joe

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: SS3 - returning datalist as array always throwing error Link to this post

    Why can't you see the error? Developing without errors is like driving blind, you're doing fine till you hit something sooner or later. Look into making sure you can see errors. (set dev mode in your _config or _ss_environment) or check your php server error log file.

    DataList will internally generate SQL, you can see what SQL it'll give out if you output Program::get()->filter(array('StateID' => $states))->sql(). Compare the sql to what you think you should be running. Run it in your db backend, see if that errors out.

  • obj63mc
    Avatar
    Community Member
    24 Posts

    Re: SS3 - returning datalist as array always throwing error Link to this post

    Hi Willr,

    I had checked the sql it is correct and when run returns the proper data.

    When I set my site to dev mode I am always getting errors of -
    [Strict Notice] Declaration of HomePage_Controller::init() should be compatible with that of Page_Controller::init()

    Note we extended Page_Controller init to allow the passing of two parameters. All works fine just whenever I switch to dev mode I cannot get these errors to ignore even though nothing is wrong. If I set my php.ini to not log strict it still shows them.

    HomePage.php -

    class HomePage_Controller extends Page_Controller {
          public function init() {         
             $css = array('file1', 'file2');
             $js = array('file1', 'file2');
             parent::init($css, $js);         
          }
    }

    Page.php -

    public function init($customCSSFile = null, $customJSFile = null) {
    parent::init();
    self::loadStyles($customCSSFile, $customJSFile);
    }

    So as you can see we are not doing anything out of the ordinary just some standard things that help us control/minify our css and js files etc...

    If I try to use ?isDev=1 that just goes into an infinite loop and uses all memory even if I give the site 2GB to use. That is related to bug - http://open.silverstripe.org/ticket/7746

    So not really sure what else to recommend. Been developing on silverstripe for 4 years now but the new SS3 ORM just doesn't seem to work like the api docs would assume to let it work.

    Anyways, thanks for the response,
    Joe

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: SS3 - returning datalist as array always throwing error Link to this post

    Exactly as the error says "Declaration of HomePage_Controller::init() should be compatible with that of Page_Controller::init()". You have to keep the same signature between all the functions.

    IMHO rather than passing things through levels of constructors use class properties.

    class Page ..

    protected $css, $js;
    ..

    class HomePage

    function init() {
    $this->css[] = "foo.css";
    $this->js[] = "bar.js";

    parent::init();
    }

  • obj63mc
    Avatar
    Community Member
    24 Posts

    Re: SS3 - returning datalist as array always throwing error Link to this post

    Hi Willr,

    I understand what the error states, my issue is that if I specifically have php not set to log/show 'Strict' warnings. silverstripe is still throwing them. Secondly we had been using this setup since SilverStripe 2.1 and up and only in 3.x versions did it start erroring out... So just not sure why that had changed.

    I will work to refactor the code but really still doesn't get to the heart of the issue of when the API logs state that on DataList you can call toNestedArray() and the sql checks out, why would core truly error out...

    I'll let you know what I see after I have refactored everything.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: SS3 - returning datalist as array always throwing error Link to this post

    I understand what the error states, my issue is that if I specifically have php not set to log/show 'Strict' warnings. silverstripe is still throwing them.

    Check SilverStripe doesn't override the error_reporting() somewhere (e.g in _config.php) and your syntax is correct.

    I didn't see any other error in your post other than that strict issue, does fixing the strict issue give you another error or a white page?

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