[Catalyst-dev] Problems with $c->model() and _comp_singular()

Jonas jonas.alves at gmail.com
Tue Aug 8 17:57:37 CEST 2006


Maybe $c->model should return the DBIC model schema if it founds one.
That way if you have a DBIC Model schema named MyApp::Model::Foo and
want to get the Bar resultset you just have to write
$c->model->resultset('Bar') instead of
$c->model('Foo')->resultset('Bar'). I know that you can write
$c->model('Foo::Bar'); but the other form is useful if you don't know
the model name. In the new instantCRUD it's possible to define the
model name in the helper but than i don't know what name the user as
chosen so i can't have it hardcoded in the base controllers. What i
did to circumvent that was to write the model name in the generated
app config.
Just an idea...

Best regards,
--Jonas

On 08/08/06, Brandon Black <blblack at gmail.com> wrote:
>
>
>
> On 8/8/06, Nathan Kurz <nate at verse.com> wrote:
> > On Tue, Aug 08, 2006 at 11:53:09AM +0100, Matt S Trout wrote:
> > > Nathan Kurz wrote:
> > > > I was just changing some things with my DBIx::Class schema's, and ran
> > > > into some problems that stumped me for a bit.  I had been calling
> > > > $c->model() and having it choose the correct model, but after I
> > > > rearranged the items in MyApp::Model::* things stopped working.
> > > >
> > > > I put in a debug statement like this:
> > > >   sub test : Local {
> > > >       my ($self, $c) = @_;
> > > >       $c->log->dumper('ref $c->model: ' . ref $c->model);
> > > >   }
> > > > And to my surprise found that $c->model made no sense at all:
> > > >   [debug] $VAR1 = 'ref $c->model: DBIx::Class::ResultSet';
> > >
> > > That makes perfect sense - it'll be finding the first
> > > MyApp::Model::* in hash key order; if that happens to be a
> > > DBIC::Schema sub-model ( i.e.  Model::DBIC::Foo) you'll get back a
> > > resultset as $c->model("DBIC::Foo") would return.
> >
> > Thanks for your quick reponse, Matt!
> >
> > Perhaps I mispoke when I said "made no sense".  I was able to figure
> > out what was happening, but didn't think that behaviour matched the
> > documentation of $c->model.  The behaviour might make sense if
> > '_comp_singular()' were named '_comp_first_in_hash()' and $c->model
> > were documented as such, but as it currently is I presumed it would
> > have died or returned nothing if multiple components matched, instead
> > of an apparently random component.
>
>
>
> The documented behavior of $c->model is that it requires either an argument,
> or a configuration setting called "default_model".  If there is any flaw in
> Catalyst.pm with regard to your issue, its that it isn't throwing an
> exception when you call $c->model with no argument and no preset
> "default_model".  The docs only indicate returning something with no
> args/default for Views, not Models, and even then only if only a single View
> exists.  With DBIC::Schema, a Model is created for every table, so you can't
> have just one Model.
>
>
> >   sub _comp_singular {
> >       my ( $c, @prefixes ) = @_;
> >       my $appclass = ref $c || $c;
> > ->    my ( $comp, $rest ) =
> > ->      map { $c->_comp_search("^${appclass}::${_}::") }
> @prefixes;
> > ->    return $comp unless $rest;
> >   }
> >
> > Do the last two lines here make sense as well?  If so, I think they
> > could maintain the same behaviour if the last line were changed to the
> > more transparent "return $rest || $comp".  For example:
> >
> >   >  perl -e 'sub foo { my ($comp, $rest) = (1,2); \
> >                         return $comp unless $rest;} \
> >               print foo(). "\n";'
> >   > 2
> >
> > But is the desired behaviour really to return the first hash-order
> > matching from component using the second element of @prefix if one is
> > found, otherwise to return the first hash-order matching item from the
> > first element of @prefix?  If it is, I think an explicit return value
> > might it clearer that this is the intended behaviour. :)
>
>
> I haven't tested it myself to see if there's a problem here, but the
> intended behavior is completely different than what you describe.  The
> intended behavior is that _comp_singular return a component name if and only
> if there's only a single matching component.  It should return nothing if
> there's more than one match.
>
> -- Brandon
>
> _______________________________________________
> Catalyst-dev mailing list
> Catalyst-dev at lists.rawmode.org
> http://lists.rawmode.org/mailman/listinfo/catalyst-dev
>
>
>



More information about the Catalyst-dev mailing list