[Catalyst] More natural access to model?

Zbigniew Lukasiak zzbbyy at gmail.com
Tue May 12 10:10:07 GMT 2009


2009/5/12 Paweł Tęcza <ptecza at uw.edu.pl>:
> Dear Catalyst users,
>
> It's my first post here, so I would like to say "Hello" to all of us! :)
>
> I'm writing my first Catalyst application for student registration and I
> have the following tables for details about studies at our university:
>
> CREATE TABLE studies (
>    id integer NOT NULL,
>    unit_id integer,
>    status integer,
>    limit_soft integer,
>    limit_hard integer
> );
>
> 'id' column is a database study identifier. It's also primary key for
> that table. 'unit_id' is an unit identifier of study, 'status' column
> says whether registration is open/suspended/closed, 'limit_soft' and
> 'limit_hard' are for student number limits.
>
> CREATE TABLE study_data (
>    study_id integer NOT NULL,
>    lang lang NOT NULL,
>    name text NOT NULL,
>    value text
> );
>
> 'study_id' column is database study identifier (please look at
> studies.id column), 'lang' column points language for (name, value)
> pair, for example 'pl', 'en', etc. 'name' column is for name of data,
> for example 'name', 'description', 'program', 'email', etc.
> ('study_id', 'lang', 'name') is a primary key for that table. Finally
> 'value' column is for data content.
>
> I think it's very simple structure and fully understandable for you,
> so it doesn't need more comments. Of course, I can also define
> table 'study_data' with many columns, for example 'name', 'description',
> 'program', 'email', etc. but I think that its simpler structure is
> better idea here, because it's more flexible. I can add new type of
> data, without changing table definition.
>
> Unfortunately, when I want to get the study details, then I need to use
> the code like below:
>
> my $study_name        = '';
> my $study_description = '';
> my $study_program     = '';
> my $study_email       = '';
>
> my @studies = $c->model('DB::Studies')->all;
> foreach my $study in (@studies) {
>    my $name  = $study.study_datas.name;
>    my $value = $study.study_datas.value;
>
>    $study_name        = $value if ($name eq 'name');
>    $study_description = $value if ($name eq 'description');
>    $study_program     = $value if ($name eq 'program');
>    $study_email       = $value if ($name eq 'email');
> }
>
> I hope you agree with me that it's not handy way...
>
> So my question is: how can I modify my model to get the study details
> in the following, more "natural" way?  Is it possibble at all?
>
> my @studies = $c->model('DB::Studies')->all;
> foreach my $study in (@studies) {
>    $study_name        = $study.study_datas.name        || '';
>    $study_description = $study.study_datas.description || '';
>    $study_program     = $study.study_datas.program     || '';
>    $study_email       = $study.study_datas.email       || '';
> }
>

Hmm - there is something missing there - according to your description
above $study.study_datas.program can not identify one value - but
rather an array of values (one for each language) isn't that true?

You might also want to change the '.' to '->'.

-- 
Zbigniew Lukasiak
http://brudnopis.blogspot.com/
http://perlalchemy.blogspot.com/



More information about the Catalyst mailing list