Breaking associations, how?

23 views
Skip to first unread message

martin

unread,
May 21, 2013, 11:11:51 PM5/21/13
to cfwh...@googlegroups.com
Tried to find an answer with Google, but I must not be using the right search terms ;-(

I've got a simple one-to-many association:

Club hasMany Teams.
Team belongsTo Club

I want to remove a team from a club without deleting the club or the team.

I figured I just get the team and set it's clubid to 0 (I also tried '') and 'save' the team.

Team = model("Team").findOneByIdAndClubid(value="#params.key#,#params.clubid#");
team.clubid = 0;
if (Team.save()){
renderWith(status="success", message="The team was removed successfully.", redirect={back="true"});
} else {
renderWith(status="error", message="There was an error removing the teams.", redirect={back="true"});
}



Team.save() throws an error.

I bet I'm just missing something obvious, right?

Thanks in advance…martin

martin

unread,
May 21, 2013, 11:20:12 PM5/21/13
to cfwh...@googlegroups.com
How about many-to-may relationships?
With an event and a team connected via eventTeam, when I delete the eventTeam record, it fails later because it was only 'soft-deleted'.

How do I handle that?

Thanks…martin

Adam Chapman

unread,
May 22, 2013, 1:55:39 AM5/22/13
to cfwh...@googlegroups.com
Hi Martin,

Use eventTeam.delete(softDelete=false) to permanently delete the database row.

Regards,
Adam

http://cfwheels.org/docs/1-1/function/delete

martin

unread,
May 22, 2013, 8:54:19 AM5/22/13
to cfwh...@googlegroups.com
Excellent. That solves the problem with breaking many-to-many relationships.

Do I have to do a custom sql solution for the one-to-many break up?

Adam Chapman

unread,
May 22, 2013, 4:45:31 PM5/22/13
to cfwh...@googlegroups.com
You shouldn't need a custom query, have a look at the "dependent" argument for your model associations.

martin

unread,
May 22, 2013, 8:45:14 PM5/22/13
to cfwh...@googlegroups.com
Perhaps I don't understand how 'dependent' work with one-to-many. I use it right now, when I delete an object that has dependencies to others…and it works great.

However, I don't see how it would be used to unassociate (not delete) an associated item. I don't want to delete either club or team, just break the relationship.

martin

unread,
May 22, 2013, 9:34:53 PM5/22/13
to cfwh...@googlegroups.com
So I tried the dynamic Finder methods:

Team = model("Team").findOneByIdAndClubid(value="#params.key#,#params.clubid#");
club = model("Club").findByKey(key=params.clubid);
if (club.removeTeam(team)){
renderWith(status="success", message="The team was removed successfully.", redirect={back="true"});
} else {
renderWith(status="error", message="There was an error removing the teams.", redirect={back="true"});
}
No luck. Then I tried updateByKey:

model("team").updateByKey(key=params.key, clubid="");

Still get an error…so that make me wonder what's going on underneath the hood. I checked the database and the clubid is just an INT that's nullable.

What am I missing?

martin

unread,
May 22, 2013, 10:25:08 PM5/22/13
to cfwh...@googlegroups.com
If I by pass wheels I can get it to work:  
team.updateProperty('clubid', params.clubid)

Baffling…and there doesn't seem to be an easy way to debug this…


Adam Chapman

unread,
May 22, 2013, 10:35:43 PM5/22/13
to cfwh...@googlegroups.com
It may depend on how your models are setup, but from the sounds of it, you just want to set the teams.clubid column to null.

If I'm not mistaken, the simplest way to achieve this (depending on how your UI works) is to pass params.team.clubid as an empty string. If you have a dropdown containing the clubs in your edit team form.. make sure there is an empty option..

You should then be able to use the bog stock scaffold style controller..
Reply all
Reply to author
Forward
0 new messages