[Catalyst] Foreign keys and DBIC
Jason Kohles
email at jasonkohles.com
Fri Feb 23 14:38:18 GMT 2007
On Feb 22, 2007, at 5:29 PM, RA Jones wrote:
> Hi folks,
>
> I'm not getting it with foreign keys in DBIC. For example, table
> users has a column called location_id, which pulls the location
> from the locations table like so:
>
> select u.user, l.location from users u, locations l where
> u.location_id = l.location_id
>
> A location has many users, so in DB::Location :
> __PACKAGE__->has_many(users => 'DB::User', 'location_id');
>
> No has_many/belongs_to mappings specified in DB::Users.
>
> Then, in a method in MyApp::Controller::Users :
> $c->stash->{user} = $c->model('DB::User')->find($id);
> $c->stash->{template} = 'users/view.tt2';
>
> But the rendered view just shows location_id, not location. I
> obviously don't quite get it and have missed something here. Do I
> have to manually map location_id to location in either M, V or C?
>
You have setup your relationships so that a location has_many users,
and that is it, the relationship only goes one way. If you want the
field in DB::User to refer back to the location, you also need a
belongs_to relationship in your user class.
> Then I read in an article called 'Catalyst vs Rails vs Django Cook
> off' that "Catalyst's DBIC ORM supports multi-column primary keys
> and can do relationship mapping just by reading the schema! You
> don't even have to bother writing any has_many belongs_to
> definitions!" Exactly what I would like to do (like
> CDBI::Loader::Relationship? eg "a brewery produces beers"), but how
> so with DBIC?
See DBIx::Class::Schema::Loader
--
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing." -- Voltaire
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070223/bc057ad9/attachment.htm
More information about the Catalyst
mailing list