21310 Posts in 5739 Topics by 2604 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 4126 Views |
-
Using DataObject method to get a single object

17 January 2009 at 10:34am Last edited: 17 January 2009 10:41am
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!
-
Re: Using DataObject method to get a single object

17 January 2009 at 10:54am
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.
-
Re: Using DataObject method to get a single object

17 January 2009 at 11:10am
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.
-
Re: Using DataObject method to get a single object

17 January 2009 at 11:45am
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/
-
Re: Using DataObject method to get a single object

17 January 2009 at 11:51am
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
-
Re: Using DataObject method to get a single object

17 January 2009 at 2:29pm
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
| 4126 Views | ||
|
Page:
1
|
Go to Top |


