Improved support for DataObjectInterface implementors in forms

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@46307 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2007-12-04 04:07:04 +00:00
parent f141fbe5ad
commit 395da98591
4 changed files with 24 additions and 6 deletions

View File

@ -1591,10 +1591,15 @@ class DataObject extends Controller implements DataObjectInterface {
*/ */
public static function get_by_id($callerClass, $id) { public static function get_by_id($callerClass, $id) {
if(is_numeric($id)) { if(is_numeric($id)) {
$tableClasses = ClassInfo::dataClassesFor($callerClass); if(singleton($callerClass) instanceof DataObject) {
$baseClass = array_shift($tableClasses); $tableClasses = ClassInfo::dataClassesFor($callerClass);
$baseClass = array_shift($tableClasses);
return DataObject::get_one($callerClass,"`$baseClass`.`ID` = $id"); return DataObject::get_one($callerClass,"`$baseClass`.`ID` = $id");
// This simpler code will be used by non-DataObject classes that implement DataObjectInterface
} else {
return DataObject::get_one($callerClass,"`ID` = $id");
}
} else { } else {
user_error("DataObject::get_by_id passed a non-numeric ID #$id", E_USER_WARNING); user_error("DataObject::get_by_id passed a non-numeric ID #$id", E_USER_WARNING);
} }

View File

@ -44,4 +44,17 @@ interface DataObjectInterface {
*/ */
function delete(); function delete();
/**
* Get the named field.
* This function is sometimes called explicitly by the form system, so you need to define it, even if you use the
* default field system.
*/
function __get($fieldName);
/**
* Save content from a form into a field on this data object.
* Since the data comes straight from a form it can't be trusted and will need to be validated / escaped.'
*/
function setCastedField($fieldName, $val);
} }

View File

@ -529,7 +529,7 @@ class Form extends ViewableData {
* Save the contents of this form into the given data object. * Save the contents of this form into the given data object.
* It will make use of setCastedField() to do this. * It will make use of setCastedField() to do this.
*/ */
function saveInto(DataObject $dataObject) { function saveInto(DataObjectInterface $dataObject) {
$dataFields = $this->fields->dataFields(); $dataFields = $this->fields->dataFields();
$lastField = null; $lastField = null;

View File

@ -96,7 +96,7 @@ class FormField extends ViewableData {
* Method to save this form field into the given data object. * Method to save this form field into the given data object.
* By default, makes use of $this->dataValue() * By default, makes use of $this->dataValue()
*/ */
function saveInto(DataObject $record) { function saveInto(DataObjectInterface $record) {
if($this->name) { if($this->name) {
$record->setCastedField($this->name, $this->dataValue()); $record->setCastedField($this->name, $this->dataValue());
} }