[Catalyst] Relationship layout

Andreas Marienborg omega at palle.net
Wed Oct 5 09:42:01 CEST 2005


You seem to mix models and controllers.. Models are where teh  
relationships go (as you obviously have gotten to work, thats good).  
Controllers use theese models to fetch the right stuff from the DB/ 
backend, and make it available to the View(s). The way to do that is  
via the "stash":

package music::C::Cd; # note the lack of CDBI here, the CDBI is  
naming convention for Models, but rarely used in controller namespaces.

sub blue : Local {
my ($self, $c) = @_;
$c->stash->{cds} = music::M::CDBI::Cd->search(title => "Blue");
$c->stash->{template} = 'cds/list.tt'; # For instance
}


then you can access cds from your template

[% WHILE (cd = cds.next) %]
<li>[% cd.title %]</li>
[% END %]

or somesuch.

I would recomend the article by our beloved Jesse
http://www.perl.com/pub/a/2005/06/02/catalyst.html

and also the documentation of the Catalystpackage itself:
http://cpansearch.perl.org/~mramberg/Catalyst-5.33/

hope that helps you a little bit along

andreas

On 5. okt. 2005, at 06.47, Will Smith wrote:

> Thanks Dan,
> I follow the instruction and have all work nicely. I need to ad one  
> line to the has_a relationship to make it work. Anyway, I still get  
> some problem:
> running the app under the built-in http, I cannot use perl script.  
> So I need to have data display on the template. So if I have:
> my $cd = music::M::CDBI::Cd->search(title=>'Blue');
>
> In a perl script I could do :
> print "The CD: ",$cd->title," written by the: ",$cd->artistid- 
> >name,"\n";
>
> But, how could I do the same thing on a template? And just to  
> confirm this: the has_a,has_many relationship are defined in the  
> myapp::M::CDBI::Mytable NOT IN myapp::C::CDBI::Mytable nor Myapp.pm
>
> Thanks
> Dan Boger <perl at peeron.com> wrote:
> On Mon, Oct 03, 2005 at 07:15:37AM -0700, Will Smith wrote:
> > Oh, my bad. Yes, I put it right after the package definition. I
> > just pasted in the wrong place. I would re-phrase my question as:
> >
> > Once I have this has-many relationship declared, how would I use
> > it? And if I have more than one relationship to this one table,
> > would I declare the same way?
> >
> > Please give me a specific line of instruction.
>
> Well, a really good example can be found here:
>
> http://wiki.class-dbi.com/index.cgi?BeginnersGuide
>
> (look in the relationships section).
>
> In general, you declare the relationships with has_a and has_many,  
> then
> when you access those fields, instead of getting a scalar value,  
> you get
> CDBI objects back:
>
> package Music::Artist;
> __PACKAGE__->has_many(cds => 'Music::CD');
> ...
>
> package Music::CD;
> __PACKAGE__->has_a(artist => 'Music::Artist');
> ...
>
> $artist = $cd->artist; # returns a Music::Artist object, for the  
> artist of that CD.
> @cds = $artist->cds; # returns an array of Music::CD objects
>
> (code written in an email client should never be executed).
>
> HTH! I'm sure the CDBI wiki and list would be able to explain it  
> better
> though.
>
> Dan
>
> -- 
>
> _______________________________________________
> Catalyst mailing list
> Catalyst at lists.rawmode.org
> http://lists.rawmode.org/mailman/listinfo/catalyst
>
> Yahoo! for Good
> Click here to donate to the Hurricane Katrina relief effort.
> _______________________________________________
> Catalyst mailing list
> Catalyst at lists.rawmode.org
> http://lists.rawmode.org/mailman/listinfo/catalyst
>




More information about the Catalyst mailing list