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

Form Errors - Trying to get property of a non-object


Reply


2 Posts   1052 Views

Avatar
Josh

SilverStripe Developer, 65 Posts

1 July 2008 at 5:18pm

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;
      
   }

:)

Avatar
Sam

Administrator, 685 Posts

7 July 2008 at 3:01pm

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().