浏览代码

Fixed bug in the JSON REST API: core/create and core/update, could not reset an external key (0)

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@2852 a333f486-631f-4898-b8df-5754b55c2be0
romainq 11 年之前
父节点
当前提交
46bfdac931
共有 1 个文件被更改,包括 14 次插入6 次删除
  1. 14 6
      application/applicationextension.inc.php

+ 14 - 6
application/applicationextension.inc.php

@@ -782,11 +782,12 @@ class RestUtils
 	 * 	 
 	 * @param string $sClass Name of the class
 	 * @param mixed $key Either search criteria (substructure), or an object or an OQL string.
+	 * @param bool $bAllowNullValue Allow the cases such as key = 0 or key = {null} and return null then
 	 * @return DBObject The object found
 	 * @throws Exception If the input structure is not valid or it could not find exactly one object
 	 * @api
 	 */
-	public static function FindObjectFromKey($sClass, $key)
+	public static function FindObjectFromKey($sClass, $key, $bAllowNullValue = false)
 	{
 		if (is_object($key))
 		{
@@ -794,10 +795,17 @@ class RestUtils
 		}
 		elseif (is_numeric($key))
 		{
-			$res = MetaModel::GetObject($sClass, $key, false);
-			if (is_null($res))
+			if ($bAllowNullValue && ($key == 0))
 			{
-				throw new Exception("Invalid object $sClass::$key");
+				$res = null;
+			}
+			else
+			{
+				$res = MetaModel::GetObject($sClass, $key, false);
+				if (is_null($res))
+				{
+					throw new Exception("Invalid object $sClass::$key");
+				}
 			}
 		}
 		elseif (is_string($key))
@@ -891,8 +899,8 @@ class RestUtils
 			$oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
 			if ($oAttDef instanceof AttributeExternalKey)
 			{
-				$oExtKeyObject = self::FindObjectFromKey($oAttDef->GetTargetClass(), $value);
-				$value = $oExtKeyObject->GetKey();
+				$oExtKeyObject = self::FindObjectFromKey($oAttDef->GetTargetClass(), $value, true /* allow null */);
+				$value = ($oExtKeyObject != null) ? $oExtKeyObject->GetKey() : 0;
 			}
 			elseif ($oAttDef instanceof AttributeLinkedSet)
 			{