[Catalyst] Foreign keys and DBIC
RA Jones
ra.jones at dpw.clara.co.uk
Sat Feb 24 12:42:50 GMT 2007
Jason Kohles wrote:
> 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.
Thanks, that did it, provided I call the item as user.location.location
in the template, otherwise I get:
MyApp::Model::DB::Location=HASH(0x449c5d0)
Will look at DBIx::Class::Schema::Loader, thanks.
--
Richard Jones
Leeds, UK
mailto:ra.jones at dpw.clara.co.uk
More information about the Catalyst
mailing list