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

Leandro Hermida softdev at leandrohermida.com
Wed Jan 19 15:33:41 GMT 2011


Hi Nigel

thanks for the help

On Wed, Jan 19, 2011 at 4:22 PM, Nigel Metheringham <
nigel.metheringham at dev.intechnology.co.uk> wrote:

> 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 que=
ry
> without touching any addresses?
> >
> > Not sure if I am using the right jargon when I said "using relationship=
s"
> 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;
>

I tried this but get Can't locate object method "person2address" via package
"MySchema::Result::Person"? What did I set up wrong?  In
MySchema::Result::Person I have for the relationship:

__PACKAGE__->has_many('person2address' =3D>
'MySchema::Result::Person2Address', 'person_id');
__PACKAGE__->many_to_many('addresses' =3D> 'person2address', 'address');



>    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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20110119/0b9=
7fe5c/attachment.htm


More information about the DBIx-Class mailing list