Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Clearing and re-adding related objects #418

xmeltrut opened this Issue · 5 comments

4 participants


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();

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


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.


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();


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


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));

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


Isn't it an issue for the behavior itself?


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
Something went wrong with that request. Please try again.