Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Form Errors - Trying to get property of a non-object

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: 994 Views
  • Josh
    Avatar
    SilverStripe Developer
    65 Posts

    Form Errors - Trying to get property of a non-object Link to this post

    Hello!

    The below code throws two errors which I am having trouble with. The first is in the GetEntrant function. If I have the $data and $form variable defined in the circle brackets - I get an error saying "FATAL ERROR: Missing argument 2 for DrawPrizePage_Controller::GetEntrant()" for both the $data and $form variable.

    The second and more important "problem" I have is getting the value from a database object. I am trying to get the MemberDataID value into a variable, from the Competitions table. It is commented in the code below.

    class DrawPrizePage_Controller extends Page_Controller {
       
       function Form() {
          return new Form($this, "Form",
                new FieldSet(
                   new TextField("CompID", "Competition Page ID")
                   ),
                   
                new FieldSet(
                   new FormAction("GetEntrant", "Draw Winners!")
                   ));
          
       }
       
       function GetEntrant($data, $form) {
          
          $CompID = $this->data['CompID'];
          
          $GetEntry = DataObject::get_one("Competition", "CompetitionPageID='{$CompID}'");      
          
          $WinnersDetails = DataObject::get_one("Member", "ID='{$GetEntry->MemberDataID}'"); // How do I get the value for the MemberDataID column which is called in $GetEntry above?
          
          return $WinnersDetails;
          
       }

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Form Errors - Trying to get property of a non-object Link to this post

    Hi subvert,

    If your Competition object has a has_one relationship like this:

    static $has_one = array(
    "MemberData" => "Member",
    );

    Then you should be able to access the related member's ID by using $GetEntry->MemberDataID, as you have.

    Can you post some more information about what errors you are getting - I assume that the code you've posted doesn't work as-is?

    PS: $CompID has a SQL-injection vulnerabililty, since $data["CompID"] is set by the browser. Use Convert::raw2sql().

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