(merged from branches/roa. use "svn log -c <changeset> -g <module-svn-path>" for detailed commit message)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60203 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-08-09 02:00:40 +00:00
parent 37d3aa1695
commit e25f44604f
8 changed files with 81 additions and 10 deletions

View File

@ -38,8 +38,10 @@ class RestfulServer extends Controller {
ContentNegotiator::disable(); ContentNegotiator::disable();
$requestMethod = $_SERVER['REQUEST_METHOD']; $requestMethod = $_SERVER['REQUEST_METHOD'];
if(!isset($this->urlParams['ClassName'])) return $this->notFound();
$className = $this->urlParams['ClassName']; $className = $this->urlParams['ClassName'];
$id = $this->urlParams['ID']; $id = (isset($this->urlParams['ID'])) ? $this->urlParams['ID'] : null;
switch($requestMethod) { switch($requestMethod) {
case 'GET': case 'GET':

View File

@ -334,7 +334,11 @@ class ViewableData extends Object implements IteratorAggregate {
if($constructor) { if($constructor) {
$fieldObj = eval($constructor); $fieldObj = eval($constructor);
$fieldObj->setVal($val); if($this->hasMethod('getAllFields')) {
$fieldObj->setVal($val, $this->getAllFields());
} else {
$fieldObj->setVal($val);
}
} }
} }

View File

@ -601,12 +601,15 @@ class DataObject extends ViewableData implements DataObjectInterface {
} }
// Divvy up field saving into a number of database manipulations // Divvy up field saving into a number of database manipulations
$manipulation = array();
if(isset($ancestry) && is_array($ancestry)) { if(isset($ancestry) && is_array($ancestry)) {
foreach($ancestry as $idx => $class) { foreach($ancestry as $idx => $class) {
$classSingleton = singleton($class); $classSingleton = singleton($class);
foreach($this->record as $fieldName => $value) { foreach($this->record as $fieldName => $fieldValue) {
if(isset($this->changed[$fieldName]) && $this->changed[$fieldName] && $fieldType = $classSingleton->fieldExists($fieldName)) { if(isset($this->changed[$fieldName]) && $this->changed[$fieldName] && $fieldType = $classSingleton->fieldExists($fieldName)) {
$manipulation[$class]['fields'][$fieldName] = $value ? ("'" . addslashes($value) . "'") : singleton($fieldType)->nullValue(); $fieldObj = $this->obj($fieldName);
if(!isset($manipulation[$class])) $manipulation[$class] = array();
if($fieldObj) $fieldObj->writeToManipulation($manipulation[$class]);
} }
} }
@ -1487,7 +1490,7 @@ class DataObject extends ViewableData implements DataObjectInterface {
$constructor = $helperPair['castingHelper']; $constructor = $helperPair['castingHelper'];
if($obj = eval($constructor)) { if($obj = eval($constructor)) {
$obj->setVal($this->$fieldName); $obj->setVal($this->$fieldName, $this->record);
} }
return $obj; return $obj;
@ -1562,6 +1565,11 @@ class DataObject extends ViewableData implements DataObjectInterface {
foreach($tableClasses as $tableClass) { foreach($tableClasses as $tableClass) {
$query->from[$tableClass] = "LEFT JOIN `$tableClass` ON `$tableClass`.ID = `$baseClass`.ID"; $query->from[$tableClass] = "LEFT JOIN `$tableClass` ON `$tableClass`.ID = `$baseClass`.ID";
$query->select[] = "`$tableClass`.*"; $query->select[] = "`$tableClass`.*";
// ask each $db field on the specific table for alterations to the query
$uninheritedDbFields = singleton($tableClass)->uninherited('db',true);
if($uninheritedDbFields) foreach($uninheritedDbFields as $fieldName => $fieldType) {
singleton($tableClass)->obj($fieldName)->addToQuery($query);
}
} }
} }
$query->select[] = "`$baseClass`.ID"; $query->select[] = "`$baseClass`.ID";

View File

@ -38,14 +38,62 @@ abstract class DBField extends ViewableData {
return $dbField; return $dbField;
} }
function setVal($value) { function setVal($value, $record = null) {
return $this->setValue($value); return $this->setValue($value);
} }
function setValue($value) { /**
* Set the value on the field.
* Optionally takes the whole record as an argument,
* to pick other values.
*
* @param mixed $value
* @param array $record
*/
function setValue($value, $record = null) {
$this->value = $value; $this->value = $value;
} }
/**
* Determines if the field has a value which
* is not considered to be 'null' in
* a database context.
*
* @return boolean
*/
function hasValue() {
return ($this->value);
}
/**
* Prepare the current field for usage in a
* database-manipulation (works on a manipulation reference).
*
* Make value safe for insertion into
* a SQL SET statement by applying addslashes() -
* can also be used to apply
* special SQL-commands to the raw value
* (e.g. for GIS functionality).
*
* @param array $manipulation
*/
function writeToManipulation(&$manipulation) {
$manipulation['fields'][$this->name] = $this->hasValue() ? "'" . addslashes($this->value) . "'" : $this->nullValue();
}
/**
* Add custom query parameters for this field,
* mostly SELECT statements for multi-value fields.
*
* By default, the ORM layer does a
* SELECT <tablename>.* which
* gets you the default representations
* of all columns.
*
* @param Query $query
*/
function addToQuery(&$query) {}
function setTable($tableName) { function setTable($tableName) {
$this->tableName = $tableName; $this->tableName = $tableName;
} }

View File

@ -1,7 +1,6 @@
<?php <?php
/** /**
* @package sapphire *
* @subpackage model
*/ */
class Double extends DBField { class Double extends DBField {

View File

@ -13,6 +13,10 @@ class Text extends DBField {
DB::requireField($this->tableName, $this->name, "mediumtext character set utf8 collate utf8_general_ci"); DB::requireField($this->tableName, $this->name, "mediumtext character set utf8 collate utf8_general_ci");
} }
function hasValue() {
return ($this->value || $this->value == '0');
}
//useed for search results show only limited contents //useed for search results show only limited contents
function LimitWordCount($numWords = 26) { function LimitWordCount($numWords = 26) {
$this->value = Convert::xml2raw($this->value); $this->value = Convert::xml2raw($this->value);

View File

@ -5,6 +5,7 @@
* @subpackage model * @subpackage model
*/ */
class Varchar extends DBField { class Varchar extends DBField {
protected $size; protected $size;
function __construct($name, $size = 50) { function __construct($name, $size = 50) {
@ -14,6 +15,11 @@ class Varchar extends DBField {
function requireField() { function requireField() {
DB::requireField($this->tableName, $this->name, "varchar($this->size) character set utf8 collate utf8_general_ci"); DB::requireField($this->tableName, $this->name, "varchar($this->size) character set utf8 collate utf8_general_ci");
} }
function hasValue() {
return ($this->value || $this->value == '0');
}
/** /**
* Return the first letter of the string followed by a . * Return the first letter of the string followed by a .
*/ */

View File

@ -590,7 +590,7 @@ class Form extends ViewableData {
if($o || !isset($object[$name . '_unchanged'])) { if($o || !isset($object[$name . '_unchanged'])) {
// Second check was the original check: save the value if we have one // Second check was the original check: save the value if we have one
if(isset($val) || $loadBlanks) { if(isset($val) || $loadBlanks) {
$field->setValue($val); $field->setValue($val, $object);
} }
} }
} }