[Catalyst] Perplexed Catalyst newbie

J. Shirley jshirley at gmail.com
Sun Apr 19 21:49:04 GMT 2009


On Mon, Apr 20, 2009 at 5:05 AM, Sean McAfee <eefacm at gmail.com> wrote:

> On Sun, Apr 19, 2009 at 12:41 PM, John Romkey <romkey at apocalypse.org>wrot=
e:
>
>> On Apr 19, 2009, at 3:22 PM, Sean McAfee wrote:
>>
>> [% FOR l IN lang; FOR s IN l.sources; s.termsets; END; END %]
>>
>> This TT snippet prints
>>
>> Quiz::Model::DB::Termset=3DHASH(0xcaa4d8)
>> ARRAY(0xca4fb4)
>>
>> So now I'm completely confused.  Any help would be massively appreciated.
>>
>>
>> This bit me a number of times when I was starting out with Catalyst.
>>
>> When you call $schema->resultset('Language') you're passing in to
>> DBIx::Class::Schema the name of a ResultSet ('Language')
>>
>> When you call $c->model('Language') you're passing in to Catalyst the na=
me
>> of a Catalyst component which is a model. When the model corresponds to a
>> DBIx::Class::Schema object it's a wrapper which passes stuff through.
>>
>> If Catalyst doesn't find a direct match for the name, it does a regular
>> expression search across components to find one which matches. This often
>> gives you unexpected results (to put it mildly). Recent revisions of
>> Catalyst have issued loud warnings when it falls back to the regex searc=
h.
>>
>> To avoid the search, you'll have to qualify the model name. The last time
>> I wrote about this I screwed up the way that should be done, so I'll try=
 to
>> do it better here. If the full name of your model is
>> Quiz::Model::DB::Language then you should pass DB::Language to $c->model=
().
>> That should avoid the regex search and get you back the resultset that
>> you're looking for rather than the Catalyst model object that you're not.
>>
>
> I changed $c->model('Language') to $c->model('DB::Language') in my
> controller, but as far as I can tell, nothing has changed.  I still get
> Quiz::Model::DB::Language objects back, not Quiz::Schema::Language object=
s.
> The snippet above produces the same output, including the oddball unbless=
ed
> array reference.
>
> I only have three schema types in my project right now, with quite
> dissimilar names, so there's not much ambiguity in a regex search, I would
> imagine.
>
> I still don't get how a DBIx::Class::ResultSet object's all() method can
> return Catalyst objects in the first place.  $c->model('Language') and
> $c->model('DB::Language') both return a DBIC ResultSet object.  Does DBIC
> have setup hooks or something that let a caller wrap returned objects?
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>
Catalyst::Model::DBIC::Schema simply looks at all available result sets and
creates a model class that is a transparent wrapper.  It should function the
exact same.

The code that does this is:

    foreach my $moniker ($self->schema->sources) {
        my $classname =3D "${class}::$moniker";
        *{"${classname}::ACCEPT_CONTEXT"} =3D sub {
            shift;
            shift->model($model_name)->resultset($moniker);
        }

    }

As you can see, all it is doing is a short-hand to
$c->model('DB')->resultset($moniker) ($moniker being the name, 'Language',
$class being 'Quiz::Model::DB')

That's how it works, but without seeing actual code in your application I'm
not sure why you are getting unexpected results.

-J
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20090420/9b84b=
45f/attachment.htm


More information about the Catalyst mailing list