0011562: adding two new tasks fails when saving lead
authorsstamer <s.stamer@metaways.de>
Tue, 19 Jan 2016 15:12:34 +0000 (16:12 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 20 Jan 2016 14:26:07 +0000 (15:26 +0100)
* Tinebase_Relation - issue creating two new relations of the
 same type

related_id was still empty, yet part of a unique key. That lead to
collision
fixed it by assigning temporary uid

https://forge.tine20.org/view.php?id=11562

Change-Id: I73313d742d6d8bb171d7df5c5800fb8990cd793f
Reviewed-on: http://gerrit.tine20.com/customers/2597
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Crm/JsonTest.php
tine20/Tinebase/Relations.php

index a65b055..b236c69 100644 (file)
@@ -513,6 +513,27 @@ class Crm_JsonTest extends Crm_AbstractTest
     }
     
     /**
+     * try to add multiple related tasks with one save
+     *
+     */
+    public function testLeadWithMultipleTasks()
+    {
+        $lead = $this->_getLead();
+        $task1 = $this->_getTask();
+        $task2 = $this->_getTask();
+        
+        
+        $leadData = $lead->toArray();
+        $leadData['relations'] = array(
+                array('type'  => 'TASK', 'related_record' => $task1->toArray()),
+                array('type'  => 'TASK', 'related_record' => $task2->toArray())
+        );
+        
+        $savedLead = $this->_getUit()->saveLead($leadData);
+        $this->assertEquals(2, count($savedLead['relations']), 'Relations missing');
+    }
+    
+    /**
      * get contact
      * 
      * @return Addressbook_Model_Contact
index 03b604d..cdee49d 100644 (file)
@@ -104,7 +104,15 @@ class Tinebase_Relations
         $toAdd = $relations->getIdLessIndexes();
         $toDel = array_diff($currentIds, $relationsIds);
         $toUpdate = array_intersect($currentIds, $relationsIds);
-        
+
+        // prevent two empty related_id s of the same relation type
+        $emptyRelatedId = array();
+        foreach ($toAdd as $idx) {
+            if (empty($relations[$idx]->related_id)) {
+                $relations[$idx]->related_id = Tinebase_Record_Abstract::generateUID();
+                $emptyRelatedId[$idx] = true;
+            }
+        }
         $this->_validateConstraintsConfig($_model, $relations, $toDel, $toUpdate);
         
         // break relations
@@ -114,7 +122,8 @@ class Tinebase_Relations
         
         // add new relations
         foreach ($toAdd as $idx) {
-            if(empty($relations[$idx]->related_id)) {
+            if(isset($emptyRelatedId[$idx])) {
+                $relations[$idx]->related_id = null;
                 $this->_setAppRecord($relations[$idx], $_doCreateUpdateCheck);
             }
             $this->_addRelation($relations[$idx]);