Ticket #1078 (closed defect: fixed)

Opened 14 months ago

Last modified 10 months ago

synchronizeWithArray fails when m-n relationship with n already existing

Reported by: isleshocky77 Owned by: jwage
Priority: major Milestone:
Component: Record Version: 0.11.0
Severity: Keywords: synchronizeWithArray
Cc: guilhermeblanco, isleshocky77 Has Test: yes
Status: Pending Core Response Has Patch: no

Description

Failing Code:

$user = new User();
$array = array();

# This will work as expected by adding a new group and a relationship
to the User
$array['Group'][] = array('name' => 'New Group');

# These will all fail with an assortment of error messages
## SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate
entry '1' for key 1
$array['Group'][] = array('id' => 1);
## SQLSTATE[23000]: Integrity constraint violation: 1048 Column
'immigrant_type_id' cannot be null
$group = Doctrine::getTable('Group')->find(1);
$array['Group'][] = $group;

$user->synchronizeWithArray($array);
$user->save(); 

Example of why this is so useful

/*
# Template
echo input_tag('user[name]');
echo input_tag('user[username]');
echo input_tag('user[Phonenumber][][phonenumber]');

echo select_tag('user[Group][][id]', array('1' => 'Group which already exists');
*/

# Action
$user = $this->getRequestParameter('user');
$User = new User();
$User->synnchronizeWithArray($user);
$User->save();

I've attached a patch to the SynchronizeTestCase.php with a failing test

Attachments

SynchronizeTestCase.php.patch (1.8 KB) - added by isleshocky77 14 months ago.
SynchronizeTestCase?.php Patch
Doctrine_Record_Synchronize_TestCase.diff (2.7 KB) - added by isleshocky77 13 months ago.
Patch for Synchronize test case to add a failing test

Change History

Changed 14 months ago by isleshocky77

Changed 13 months ago by jwage

  • owner changed from romanb to guilhermeblanco

Changed 13 months ago by guilhermeblanco

synchronizeWithArray should only be used when you are manipulating an existent record.

Since you're dealing with a new object (create a new User), the method that should be used is fromArray. I changed it and created a sample, which is failing too. Maybe this is due to Windows tests that are broken, but I cannot confirm it. I'll take a deep look at it later with more information about this issue.

If you want to go ahead and try to change your code to use fromArray, feel free to publish the results to me.

Changed 13 months ago by isleshocky77

I will rewrite my test as well. But I know fromArray was not working because I started with fromArray and when it didn't work correctly I moved to syncWithArray. It's good to get confirmation on what I'm supposed to be using though. I was not sure if were supposed to be using syncWithArray, from Array, or merge.

Changed 13 months ago by isleshocky77

  • cc guilhermeblanco, isleshocky77 added

I've created a new ticket #1117 which adds some Test coverage for the FromArray? method. It as pass when adding a new n-m relationship, but fails when adding a new n-m relationship to an existing m record. I still believe the SynchronizeWithArray? method has this same functionality problem so I've created a new patch which I believe is pretty similar to the one attached to this ticket with a little different commenting. I think this issue should be addressed for the exception which is thrown when saving.

Changed 13 months ago by isleshocky77

Patch for Synchronize test case to add a failing test

Changed 13 months ago by isleshocky77

  • has_patch unset
  • mystatus set to Pending Core Response
  • has_test set

Changed 12 months ago by jwage

  • owner changed from guilhermeblanco to jwage
  • version changed from 0.10 to 0.11
  • milestone changed from 0.11.3 to 0.11.1

Changed 12 months ago by jwage

  • status changed from new to closed
  • resolution set to fixed

(In [4580]) fixes #1078 fixes #1117

Changed 10 months ago by anonymous

  • milestone New deleted

Milestone New deleted

Note: See TracTickets for help on using tickets.