Jump to:

22979 Posts in 11815 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Using DataObject method to get a single object

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 5029 Views
  • fishe
    Avatar
    Community Member
    42 Posts

    Using DataObject method to get a single object Link to this post

    I have a form which relates to a PageType called GarmentPage. When the user submits the form it sends the associated ID variable as a URL variable (/blahblah?id=x)

    I have a method in my new extended userform class which is supposed to take this ID and return a data object of that particular GarmentPage page - so then on the page which displays the form I can output details from the object's variables.

    Here is the code for the method in the userform controller:

    class UserDefinedFormGarment_Controller extends UserDefinedForm_Controller {
       function OutputGarmentDetails() {
          $newID = $_GET['id'];
          
          return DataObject::get_one("GarmentPage", "GarmentPage.ID == $newID");
       }
    }

    This results in a warning "Unknown class passed as parameter". The $newID is correctly set with the ID of the corresponding GarmentPage object.

    What am I doing wrong here? Is there a better way to do this?

    Also, I'm not quite sure of the best way to output the object's variables in the template corresponding to the form output. I can use something like the following?...

    <% control OutputGarmentDetails %>
    <p>$Variable1</p>
    <% end_control %>

    Thanks guys!

  • UncleCheese
    Avatar
    4085 Posts

    Re: Using DataObject method to get a single object Link to this post

    Weird.. are you sure GarmentPage is a valid class? Run a db/build?flush=1 to rebuild the Manifest.

    Try

    return DataObject::get_by_id("GarmentPage", $newID);

    Also, as a matter of good practice, you probably want to check is_numeric($newID) before you pass it along to the database. Especially if that ID is coming in through the URL. Yikes.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Using DataObject method to get a single object Link to this post

    Also, the double equal notation is not valid SQL.

    The corrected code is DataObject::get_one("GarmentPage", "GarmentPage.ID = $newID");, but it is functionally equivalent to the get_by_id notation.

  • fishe
    Avatar
    Community Member
    42 Posts

    Re: Using DataObject method to get a single object Link to this post

    Thanks for the help guys. If I use this: return DataObject::get_by_id("GarmentPage", $newID);
    I get the following error:
    [User Error] Object::__call() Method 'forTemplate' not found in class 'GarmentPage'

    However, if I use: DataObject::get_one("GarmentPage", "GarmentPage.ID = $newID");
    I get the following error:
    [User Error] Couldn't run query: SELECT `SiteTree_Live`.*, `GarmentPage_Live`.*, `SiteTree_Live`.ID, if(`SiteTree_Live`.ClassName,`SiteTree_Live`.ClassName,'SiteTree') AS RecordClassName FROM `SiteTree_Live` LEFT JOIN `GarmentPage_Live` ON `GarmentPage_Live`.ID = `SiteTree_Live`.ID WHERE (GarmentPage.ID = 14) AND (`SiteTree_Live`.ClassName IN ('GarmentPage')) ORDER BY Sort LIMIT 1 Unknown column 'GarmentPage.ID' in 'where clause'

    Here is my GarmentPage.php:

    <?php

    class GarmentPage extends Page {
       
       public static $db = array(
          'GarmentName' => 'Text',
          'GarmentPrice' => 'Text',
          'GarmentSizes' => 'Text',
          'GarmentDescription' => 'Text'
       );
       
       public static $has_one = array(
          'GarmentSwatch1' => 'Image',
          'GarmentSwatch2' => 'Image',
          'GarmentSwatch3' => 'Image'
       );
       
       function getCMSFields() {
          $fields = parent::getCMSFields();
             
          $fields->addFieldToTab('Root.Content.Garment', new HeaderField('Garment 1'));   
          $fields->addFieldToTab('Root.Content.Garment', new TextField('GarmentName','Garment Name'));
          $fields->addFieldToTab('Root.Content.Garment', new TextareaField('GarmentDescription','Garment Description'));
       $fields->addFieldToTab('Root.Content.Garment', new TextField('GarmentSizes','Garment Sizes Available'));
          $fields->addFieldToTab('Root.Content.Garment', new CurrencyField('GarmentPrice','Garment Price'));
       
       $fields->addFieldToTab('Root.Content.Swatches', new ImageField('GarmentSwatch1'));
       $fields->addFieldToTab('Root.Content.Swatches', new ImageField('GarmentSwatch2'));
       $fields->addFieldToTab('Root.Content.Swatches', new ImageField('GarmentSwatch3'));
       
          return $fields;
       }
    }

    class GarmentPage_Controller extends Page_Controller {
    }
    ?>

    I have rebuilt the database. Could the issue be related to the file locations of the UserDefinedFormGarment.php and the GarmentPage.php? GarmentPage.php is in /mysite/code/ however UserDefinedFormGarment.php is in with the original file in /userforms/code/

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Using DataObject method to get a single object Link to this post

    Ah, ok DataObject::get_one("GarmentPage", "GarmentPage.ID = $newID"); should actually be:

    DataObject::get_one("GarmentPage", "ID = $newID");

    But then you should get the same error as for get_by_id

    What does your template look like? You should be doing what you posted:

    <% control OutputGarmentDetails %>
    <p>$Variable1</p>
    <% end_control %>

    But from your error, it sounds like you might be doing:

    $OutputGarmentDetails

  • fishe
    Avatar
    Community Member
    42 Posts

    Re: Using DataObject method to get a single object Link to this post

    Thanks Hamish!

    Problem solved. But I soon realised I really need to make a custom form to do what I want. Ugh now I have issues with removing userforms and may have damaged my SS installation...see topic post in Other Modules section. I should have just left it sitting there

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