<br><br><div><span class="gmail_quote">On 8/8/06, <b class="gmail_sendername">Nathan Kurz</b> <<a href="mailto:nate@verse.com">nate@verse.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Tue, Aug 08, 2006 at 11:53:09AM +0100, Matt S Trout wrote:<br>> Nathan Kurz wrote:<br>> > I was just changing some things with my DBIx::Class schema's, and ran<br>> > into some problems that stumped me for a bit. I had been calling
<br>> > $c->model() and having it choose the correct model, but after I<br>> > rearranged the items in MyApp::Model::* things stopped working.<br>> ><br>> > I put in a debug statement like this:
<br>> > sub test : Local {<br>> > my ($self, $c) = @_;<br>> > $c->log->dumper('ref $c->model: ' . ref $c->model);<br>> > }<br>> > And to my surprise found that $c->model made no sense at all:
<br>> > [debug] $VAR1 = 'ref $c->model: DBIx::Class::ResultSet';<br>><br>> That makes perfect sense - it'll be finding the first<br>> MyApp::Model::* in hash key order; if that happens to be a<br>> DBIC::Schema sub-model (
i.e. Model::DBIC::Foo) you'll get back a<br>> resultset as $c->model("DBIC::Foo") would return.<br><br>Thanks for your quick reponse, Matt!<br><br>Perhaps I mispoke when I said "made no sense". I was able to figure
<br>out what was happening, but didn't think that behaviour matched the<br>documentation of $c->model. The behaviour might make sense if<br>'_comp_singular()' were named '_comp_first_in_hash()' and $c->model<br>were documented as such, but as it currently is I presumed it would
<br>have died or returned nothing if multiple components matched, instead<br>of an apparently random component.</blockquote><div><br><br>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
<a href="http://Catalyst.pm">Catalyst.pm</a> 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.
<br> </div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> sub _comp_singular {<br> my ( $c, @prefixes ) = @_;<br> my $appclass = ref $c || $c;
<br>-> my ( $comp, $rest ) =<br>-> map { $c->_comp_search("^${appclass}::${_}::") } @prefixes;<br>-> return $comp unless $rest;<br> }<br><br>Do the last two lines here make sense as well? If so, I think they
<br>could maintain the same behaviour if the last line were changed to the<br>more transparent "return $rest || $comp". For example:<br><br> > perl -e 'sub foo { my ($comp, $rest) = (1,2); \<br> return $comp unless $rest;} \
<br> print foo(). "\n";'<br> > 2<br><br>But is the desired behaviour really to return the first hash-order<br>matching from component using the second element of @prefix if one is<br>found, otherwise to return the first hash-order matching item from the
<br>first element of @prefix? If it is, I think an explicit return value<br>might it clearer that this is the intended behaviour. :)</blockquote><div><br>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.
<br></div><br></div>-- Brandon<br>