[Dbix-class] deleting many-to-many relationship entries in one query

Nigel Metheringham nigel.metheringham at dev.intechnology.co.uk
Wed Jan 19 15:22:48 GMT 2011


On 19 Jan 2011, at 15:05, Leandro Hermida wrote:

> Couldn't find the answer to this simple question.... maybe not looking in the right place
> 
> I have a standard many-to-many setup:
> 
> person ------< person2address >------- address
> 
> And have defined it in the standard documented way with DBIC.  How do I in my code, where I have an instance of the $person object and using relationships, delete all this person's person2address entries in one query without touching any addresses? 
> 
> Not sure if I am using the right jargon when I said "using relationships" like I saw in the manual, what I mean is that I feel this style:
> 
> $person->cars->delete();

[the example I just deleted here served only to confuse me as to what you meant]

> How would I do it with a many-to-many just to delete the relationship entries?

 1. Delete the bridging relationship
	$person->person2address->delete;
    or
	$person->person2address_rs->delete; # works even if prefetch
    or
        $person->delete_related('person2address);

 2. Use the additional many-to-many methods:-
	$person->set_addresses([]);

	http://search.cpan.org/perldoc?DBIx::Class::Relationship::Base#set_$rel


	Nigel.
--
[ Nigel Metheringham             Nigel.Metheringham at InTechnology.com ]
[ - Comments in this message are my own and not ITO opinion/policy - ]







More information about the DBIx-Class mailing list