Skip to content

Instantly share code, notes, and snippets.

@sminnee
Created April 21, 2013 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sminnee/5428083 to your computer and use it in GitHub Desktop.
Save sminnee/5428083 to your computer and use it in GitHub Desktop.
commit 0d8aa69631cea692f94ea3b286a58cabf4fc7c7b
Author: Sam Minnee <sam@silverstripe.com>
Date: Sun Apr 21 13:18:18 2013 +1200
NEW: Added DataObject::getQueriedDatabaseFields() as faster alternative to toMap()
API: CompositeDBField::setValue() may be passed an object as its second argument, in addition to array.
These changes provide a 15% - 20% performance improvement, and as such justify an small API change in the 3.0 branch. It will likely affect anyone who has created their own composite fields, which is fortunately not all that common.
diff --git a/docs/en/changelogs/3.0.5.md b/docs/en/changelogs/3.0.5.md
new file mode 100644
index 0000000..beffa8b
--- /dev/null
+++ b/docs/en/changelogs/3.0.5.md
@@ -0,0 +1,5 @@
+# 3.0.4 (Not yet released)
+
+## Upgrading
+
+ * If you have created your own composite database fields, then you shoulcd amend the setValue() to allow the passing of an object (usually DataObject) as well as an array.
\ No newline at end of file
diff --git a/model/DataObject.php b/model/DataObject.php
index 328d31f..be9ccd2 100644
--- a/model/DataObject.php
+++ b/model/DataObject.php
@@ -731,6 +731,18 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
}
/**
+ * Return all currently fetched database fields.
+ *
+ * This function is similar to toMap() but doesn't trigger the lazy-loading of all unfetched fields.
+ * Obviously, this makes it a lot faster.
+ *
+ * @return array The data as a map.
+ */
+ public function getQueriedDatabaseFields() {
+ return $this->record;
+ }
+
+ /**
* Update a number of fields on this object, given a map of the desired changes.
*
* The field names can be simple names, or you can use a dot syntax to access $has_one relations.
diff --git a/model/fieldtypes/CompositeDBField.php b/model/fieldtypes/CompositeDBField.php
index 2cdf180..979bba6 100644
--- a/model/fieldtypes/CompositeDBField.php
+++ b/model/fieldtypes/CompositeDBField.php
@@ -118,7 +118,7 @@ interface CompositeDBField {
* parameter.
*
* @param DBField|array $value
- * @param array $record Map of values loaded from the database
+ * @param DataObject|array $record An array or object that this field is part of
* @param boolean $markChanged Indicate wether this field should be marked changed.
* Set to FALSE if you are initializing this field after construction, rather
* than setting a new value.
diff --git a/model/fieldtypes/Money.php b/model/fieldtypes/Money.php
index 30aeb5a..42ef083 100644
--- a/model/fieldtypes/Money.php
+++ b/model/fieldtypes/Money.php
@@ -103,6 +103,11 @@ class Money extends DBField implements CompositeDBField {
}
public function setValue($value, $record = null, $markChanged = true) {
+ // Convert an object to an array
+ if($record && $record instanceof DataObject) {
+ $record = $record->getQueriedDatabaseFields();
+ }
+
// @todo Allow resetting value to NULL through Money $value field
if ($value instanceof Money && $value->exists()) {
$this->setCurrency($value->getCurrency(), $markChanged);
diff --git a/view/ViewableData.php b/view/ViewableData.php
index 97fb99e..d2bc757 100644
--- a/view/ViewableData.php
+++ b/view/ViewableData.php
@@ -374,7 +374,7 @@ class ViewableData extends Object implements IteratorAggregate {
}
$valueObject = Object::create_from_string($castConstructor, $fieldName);
- $valueObject->setValue($value, ($this->hasMethod('toMap') ? $this->toMap() : null));
+ $valueObject->setValue($value, $this);
$value = $valueObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment