Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearing and re-adding related objects #418

Open
xmeltrut opened this issue Jul 17, 2012 · 5 comments
Open

Clearing and re-adding related objects #418

xmeltrut opened this issue Jul 17, 2012 · 5 comments

Comments

@xmeltrut
Copy link

Lets say you have a many to many relationship with with Person and Friend.

$friend1 = FriendQuery::create()->findPk(1);
$friend2 = FriendQuery::create()->findPk(2);

You can add these to a person object and save it.

$person = new Person();
$person->addFriend($friend1);
$person->addFriend($friend2);
$person->save();

Works fine. However, if I then clear off all the friends, and re-add them.

$person->clearFriends();
$person->addFriend($friend1);
$person->addFriend($friend2);
$person->save();

Presuming I am using PersonId/FriendId as the primary key on the cross reference table, I will get an error.

1062 Duplicate entry '277-31' for key 'PRIMARY'

I think Propel should manage this a little more intelligently. Either by deleting the existing cross reference records before trying to add the new ones, or by knowing what cross references are in there already so it doesn't try and duplicate them.

The use case for this is having a form with a series of checkboxes to indicate relationships - hence why you need to clear all the relationships out and then re-add them.

@havvg
Copy link
Member

havvg commented Jul 17, 2012

Part of the methods (clearFriends) documentation:

 * This does not modify the database; however, it will remove any associated objects, causing
 * them to be refetched by subsequent calls to accessor method.

You should use setFriends with a previously created PropelObjectCollection only containing the checked friends.
This will remove unchecked friends and insert new ones upon $person->save();

@xmeltrut
Copy link
Author

Ok, that makes more sense. However, I get the same result doing it that way.

@xmeltrut
Copy link
Author

Presuming the cross reference table is called Friendships, I can resolve the issue by adding the following line to the start.

$person->setFriendships(new \PropelCollection());

Then re-adding all the friends.

$person->setFriendships(new \PropelCollection());
$friends = FriendQuery::create()->findPks(array(1, 2));
$person->setFriends($friends);

Would be nice if Propel handled the cross reference table though.

@willdurand
Copy link
Contributor

Isn't it an issue for the behavior itself?

@marcj
Copy link
Member

marcj commented Feb 25, 2013

Looks like that is related to #603, isn't it? @xmeltrut, we've fixed a bug related to those set<Relation>s() method, can you check please if this bug still persists with the newest version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants