Commits

Scott Blevins committed 69d1b4f

Changed all the spots that assumed an integer for ID, validated it, or cast it as such. Added a spot in the config for the use of UUIDs. The next step is handling the generation of UUIDs within the Datamapper Library itself as required.

Comments (0)

Files changed (2)

application/config/datamapper.php

 $config['auto_populate_has_many'] = FALSE;
 $config['auto_populate_has_one'] = FALSE;
 $config['all_array_uses_ids'] = FALSE;
+// Set this to true to use UUIDs instead of integer IDs. (varchar (36))
+$config['use_uuids'] = FALSE;
 // set to FALSE to use the same DB instance across the board (breaks subqueries)
 // Set to any acceptable parameters to $CI->database() to override the default.
 $config['db_params'] = '';

application/libraries/datamapper.php

 					'matches' => array(),
 					'intval' => array('id')
 				);
+				if ($this->use_uuids)
+				{
+					// Overwrite this for UUIDs
+					$this->_field_tracking['intval'] = array();
+				}
 
 				// Convert validation into associative array by field name
 				$associative_validation = array();
 						'field' => 'id',
 						'rules' => array('integer')
 					);
+					if ($this->use_uuids)
+					{
+						// Overwrite this for UUIDs
+						$associative_validation['id']['rules'] = array();
+					}
 				}
 
 				$this->validation = $associative_validation;
 						( ! isset($this->validation[$related]) || // nor does the related have a validation key or...
 							! isset($this->validation[$related]['get_rules'])) ) // a get_rules key
 					{
-						// assume an int
-						$this->_field_tracking['intval'][] = $field;
+						if (!$this->use_uuids)
+						{
+							// assume an int
+							$this->_field_tracking['intval'][] = $field;
+						}
 					}
 				}
 
 
 		// Clear object properties to set at default values
 		$this->clear();
-
-		if( ! empty($id) && is_numeric($id))
-		{
-			$this->get_by_id(intval($id));
-		}
+		
+		if ($this->use_uuids)
+		{
+			// Don't cast as integer if a UUID
+			if( ! empty($id))
+			{
+				$this->get_by_id($id);
+			}
+		}
+		else
+		{
+			if( ! empty($id) && is_numeric($id))
+			{
+				$this->get_by_id(intval($id));
+			}
+		}
+		
 	}
 
 	// --------------------------------------------------------------------
 	{
 		foreach($objects as $index => $o)
 		{
-			if(is_int($index))
+			if(is_int($index) || $this->use_uuids)
 			{
 				$rf = $related_field;
 			}
 			$success = TRUE;
 			foreach($object as $rk => $o)
 			{
-				if(is_int($rk))
+				if(is_int($rk) || $this->use_uuids)
 				{
 					$rk = $related_field;
 				}
 
 			foreach ($object as $rel_field => $obj)
 			{
-				if (is_int($rel_field))
+				if (is_int($rel_field) || $this->use_uuids)
 				{
 					$rel_field = $related_field;
 				}
 				{
 					foreach ($obj as $r_f => $o)
 					{
-						if (is_int($r_f))
+						if (is_int($r_f) || $this->use_uuids)
 						{
 							$r_f = $rel_field;
 						}
 			return FALSE;
 		}
 
+		// ! Scott: TODO: Possibly handle for UUIDs ! //
 		// allow for simple (old-style) associations
 		if (is_int($name))
 		{