On Mon, Apr 20, 2009 at 5:05 AM, Sean McAfee <[email protected]> wrote:

> On Sun, Apr 19, 2009 at 12:41 PM, John Romkey <[email protected]>wrote:
>
>> 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=HASH(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 name
>> 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 search.
>>
>> 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 objects.
> The snippet above produces the same output, including the oddball unblessed
> 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: [email protected]
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/[email protected]/
> 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 = "${class}::$moniker";
        *{"${classname}::ACCEPT_CONTEXT"} = 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
_______________________________________________
List: [email protected]
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/

Reply via email to