Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] get_one doesn't compare my data

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

Page: 1
Go to End
Author Topic: 686 Views
  • quanto
    Avatar
    Community Member
    79 Posts

    [SOLVED] get_one doesn't compare my data Link to this post

    I have the very simple class:

    class ActionCode_Controller extends Page_Controller
    {
    function ActionCodeForm() {
    ...
    }

    function CheckCode($data, $form) {
    $test = DataObject::get_one("ActionCode", "`Code`='".$data['Code']."'");
    if($test){
    echo "yes";
    }
    else {
    echo "no";
    }
    }
    }

    I try to get the Code from the database. If it's in (temporary) shout yes, if it's not (temporary) shout no.
    But it always shouts no (returns false) Even if I put in `Code`= '1234'.

    What am I doing wrong?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: [SOLVED] get_one doesn't compare my data Link to this post

    Couple things

    * DataObject::get_one("ActionCode", "`Code`='".$data['Code']."'"); . If data is coming from a form this could introduce a exploit on your site.
    You should write it as DataObject::get_one("ActionCode", "\"Code\" = '". Convert::raw2sql($data['Code']) ."'"); Also note backticks are no longer recommended.

    * Try a simple DataObject::get_one("ActionCode"); and see if that returns anything.

    * Use ?showqueries=1 to see what SQL it's executing for that select and try and run it in your database directly, see if that returns anything.

  • quanto
    Avatar
    Community Member
    79 Posts

    Re: [SOLVED] get_one doesn't compare my data Link to this post

    * DataObject::get_one("ActionCode", "`Code`='".$data['Code']."'"); . If data is coming from a form this could introduce a exploit on your site.
    You should write it as DataObject::get_one("ActionCode", "\"Code\" = '". Convert::raw2sql($data['Code']) ."'"); Also note backticks are no longer recommended.

    This still returns false. What do you mean with 'backticks'?

    * Try a simple DataObject::get_one("ActionCode"); and see if that returns anything.

    This one returns true

    * Use ?showqueries=1 to see what SQL it's executing for that select and try and run it in your database directly, see if that returns anything.

    This one doesn't return anything. Not in Dev mode, neither in live mode.

  • omarkohl
    Avatar
    Community Member
    30 Posts

    Re: [SOLVED] get_one doesn't compare my data Link to this post

    Try Debug::dump($test) to see if there is anything useful inside. Backtick ist this: `(what you put around the first Code).

    Debug::dump() is probably my favourite feature ;-)

  • quanto
    Avatar
    Community Member
    79 Posts

    Re: [SOLVED] get_one doesn't compare my data Link to this post

    I already found the error:

    The class ActionCode extended Page, instead of DataObject, so the query pointed to Sitetree, and not to the table ActionCode

    Also I get now the right error output by putting the following code in __config.php:

    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    Director::set_environment_type("dev");

    Thanks all for the support.

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