[Dbix-class] many_to_many data access

Robyn Jonahs learn.catalyst at gmail.com
Thu Jul 12 08:15:04 GMT 2012


Hi thanks. I will try it.

The cities, states and country relationships are all many_to_many so once I
have one, I can do the others.

RJ


On Thu, Jul 12, 2012 at 3:48 AM, Patrick Meidl <patrick at pantheon.at> wrote:

> On Wed, Jul 11 2012, Robyn Jonahs <learn.catalyst at gmail.com> wrote:
>
> > I was trying to get some many_to_many data into a FormFU select
> > element but I am having trouble. I think that it is time to ask the
> > pros for help.
> >
> > I was staying similar to the online tutorial. In my add and edit sub
> > routines, I have the following code (similar to the online tutorial)
> >
> >         # Stores
> >         my @store_objs =3D $c->model("DB::Store")->all();
> >         my @stores;
> >         foreach (sort {$a->store cmp $b->store} @store_objs) {
> >         #  push(@stores, [$_->id, $_->store]);
> >           push(@stores, [$_->id, $_->full_address, $_->store]);
> >         }
> >         my $select3 =3D $form->get_element({name =3D> 'stores'});
> >         $select3->options(\@stores);
> >
> > So I want to get a list of all the values in a table called stores and
> > stick them into a select element in FormFU. I can do this fine. What I
> > wanted to do was to give it the full address of the store bringing in
> > the state, city and country from many_to_many relationships with these
> > tables.  I was able to do this in another test application but the
> > relationships were has_many not many_to_many. I am stumped on how to
> > do this.
> >
> > The store model is simple
> >
> > __PACKAGE__->add_columns(
> >   "id",
> >   { data_type =3D> "integer", is_auto_increment =3D> 1, is_nullable =3D=
> 0 },
> >   "store",
> >   { data_type =3D> "varchar", is_nullable =3D> 0, size =3D> 255 },
> >   "notes",
> >   { data_type =3D> "varchar", is_nullable =3D> 1, size =3D> 255 },
> > );
> >
> > and it contains
> >
> > __PACKAGE__->has_many(
> >   "store_cities",
> >   "ProductTracker::Schema::Result::StoreCity",
> >   { "foreign.store_id" =3D> "self.id" },
> >   { cascade_copy =3D> 0, cascade_delete =3D> 0 },
> > );
> >  ...
> >
> > __PACKAGE__->many_to_many("cities", "store_cities", "city");
> >
> > And I tried to do this...
> > #
> > # Row-level helper methods
> > #
> > =3Dhead2 full_address experimental
> >
> >   Get the city, state and country that correspond to a specific Store ID
> >
> > =3Dcut
> > sub full_address {
> >     my ($self) =3D @_;
> >
> >     my $cities =3D $self->store_cities;
> >     return $self->store . ' ' . $cities->city->city;
> >
> > #    return $self->store . ' ' . $self->city->city . ', ' .
> > $self->states->abb . ' ' . $self->countries->abb;
> > }
>
> there are several errors in this method.
>
> first, to get the cities associated to the store, use the many_to_many
> relationship you defined; if you have such a relationship, you usually
> never use the bridging table (store_cities in your example) directly.
>
> second, since you are dealing with a many_to_many, there might be
> multiple cities associated to a store, you have have to deal with this.
> so your code should look something like this:
>
> --8<---------------------------------------------------------------
>
> sub full_address {
>   my ($self) =3D @_;
>
>   my $result =3D $self->store;
>
>   my @cities =3D $self->cities;
>   foreach my $city (@cities) {
>     $result .=3D sprintf(' | %s, %s %s', $city->city,
>       $self->states->abb, $self->countries->abb);
>   }
>
>   return $result;
> }
>
> --8<---------------------------------------------------------------
>
> not sure about the calls to states and countries, you didn't include
> the code what these methods return; look like many_to_many to me too, or
> rather belongs_to relations on City? so I'm speculating that your code
> will look like
>
>     $result .=3D sprintf(' | %s, %s %s', $city->city,
>       $city->state->abb, $city->country->abb);
>
> HTH
>
>     patrick
>
> --
> Patrick Meidl ........................ patrick at pantheon.at
> Vienna, Austria ...................... http://gplus.to/pmeidl
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20120712/ee7=
d1ac6/attachment.htm


More information about the DBIx-Class mailing list