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

Nathan Kurz nate at verse.com
Tue Aug 8 18:18:50 CEST 2006


On Tue, Aug 08, 2006 at 10:34:45AM -0500, Brandon Black wrote:
> 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.

OK, thanks for the clarification, and indeed an exception was what I
would have expected. There is some funniness in the documentation
which is presumed was simply s/view/model/, but I guess is a little
more complex.  And I hadn't realized that each table would be
considered a model.  Doc patch attached.

>  sub _comp_singular {
> >      my ( $c, @prefixes ) = @_;
> >      my $appclass = ref $c || $c;
> >->    my ( $comp, $rest ) =
> >->      map { $c->_comp_search("^${appclass}::${_}::") } @prefixes;
> >->    return $comp unless $rest;
> >  }
>
> 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.

Unfortunately, in the absence of an explicit return value,
_comp_singular() returns the value of the last expression evaluated,
which in this case is $rest: hence "$rest || $comp".  If it is going
to work as you wrote, it needs to be reformulated.  At the least it
needs an explicit return statement, but I think it needs something
other than map { $c->_comp_search } as well.

--nate

--------------------------------------------------------------------
--- Catalyst.pm~        2006-08-08 00:21:30.000000000 -0600
+++ Catalyst.pm 2006-08-08 10:13:37.000000000 -0600
@@ -500,8 +500,8 @@
 
     $c->model('Foo')->do_stuff;
 
-If the name is omitted, it will look for a config setting 'default_model',
-or check if there is only one view, and return it if that's the case.
+If the name is omitted, it will look for a config setting 'default_model'.  
+Either a name or 'default_model' must be specified.
 
 =cut



More information about the Catalyst-dev mailing list