Skip to content
This repository
Browse code

BUG trac 7482 couldnt publish composite fields to live

When publishing to live, DataObject#forceChange is called, which wasnt correctly loading
in fields that were lazy (unloaded) if those fields were from composite fields
like Money. The end result is that any Money values would be forced to null on
publish to live

Also changes the API of the (internal, protected) loadLazyFields method so that
not passing a class argument just unlazys all lazy fields regardless of source table
  • Loading branch information...
commit e8cd675c94920f4508d64e93ae1bc9358a8e4b4c 1 parent 6992863
Hamish Friedlander authored July 18, 2012

Showing 1 changed file with 19 additions and 11 deletions. Show diff stats Hide diff stats

  1. 30  model/DataObject.php
30  model/DataObject.php
@@ -679,13 +679,7 @@ public function data() {
679 679
 	 * @return array The data as a map.
680 680
 	 */
681 681
 	public function toMap() {
682  
-		foreach ($this->record as $key => $value) {
683  
-			if (strlen($key) > 5 && substr($key, -5) == '_Lazy') {
684  
-				$this->loadLazyFields($value);
685  
-				break;
686  
-			}
687  
-		}
688  
-
  682
+		$this->loadLazyFields();
689 683
 		return $this->record;
690 684
 	}
691 685
 
@@ -847,13 +841,16 @@ public function merge($rightObj, $priority = 'right', $includeRelations = true,
847 841
 	 * if they are not already marked as changed.
848 842
 	 */
849 843
 	public function forceChange() {
  844
+		// Ensure lazy fields loaded
  845
+		$this->loadLazyFields();
  846
+
850 847
 		// $this->record might not contain the blank values so we loop on $this->inheritedDatabaseFields() as well
851 848
 		$fieldNames = array_unique(array_merge(array_keys($this->record), array_keys($this->inheritedDatabaseFields())));
852 849
 		
853 850
 		foreach($fieldNames as $fieldName) {
854 851
 			if(!isset($this->changed[$fieldName])) $this->changed[$fieldName] = 1;
855 852
 			// Populate the null values in record so that they actually get written
856  
-			if(!$this->$fieldName) $this->record[$fieldName] = null;
  853
+			if(!isset($this->record[$fieldName])) $this->record[$fieldName] = null;
857 854
 		}
858 855
 		
859 856
 		// @todo Find better way to allow versioned to write a new version after forceChange
@@ -1954,13 +1951,24 @@ public function getAllFields() {
1954 1951
 	}
1955 1952
 
1956 1953
 	/**
1957  
-	 * Loads all the stub fields than an initial lazy load didn't load fully.
  1954
+	 * Loads all the stub fields that an initial lazy load didn't load fully.
1958 1955
 	 *
1959 1956
 	 * @param tableClass Base table to load the values from. Others are joined as required.
  1957
+	 *                   Not specifying a tableClass will load all lazy fields from all tables.
1960 1958
 	 */
1961 1959
 	protected function loadLazyFields($tableClass = null) {
1962  
-		// Smarter way to work out the tableClass? Should the functionality in toMap and getField be moved into here?
1963  
-		if (!$tableClass) $tableClass = $this->ClassName;
  1960
+		if (!$tableClass) {
  1961
+			$loaded = array();
  1962
+
  1963
+			foreach ($this->record as $key => $value) {
  1964
+				if (strlen($key) > 5 && substr($key, -5) == '_Lazy' && !array_key_exists($value, $loaded)) {
  1965
+					$this->loadLazyFields($value);
  1966
+					$loaded[$value] = $value;
  1967
+				}
  1968
+			}
  1969
+
  1970
+			return;
  1971
+		}
1964 1972
 
1965 1973
 		$dataQuery = new DataQuery($tableClass);
1966 1974
 

0 notes on commit e8cd675

Please sign in to comment.
Something went wrong with that request. Please try again.