FAQ
Short version: we're wondering how a method from inside the SCHEMA can
return other
objects besides the one that got passed in. That is, we have $obj
(with its own ID
number) but we want to FIND a different object -- same type, but with
a different
ID number. What's the mechanism for that, inside the schema package?

Long version:

We've got a schema defined for hierarchical groups:

MyApp::Schema::DB::Result::Group
__PACKAGE__->add_columns(
??"id",
??{ data_type => "INT", default_value => undef, is_nullable => 0, size => 11 },
??"name",
??{
?? ?data_type => "VARCHAR",
?? ?default_value => undef,
?? ?is_nullable => 0,
?? ?size => 90,
??},
??"parent",
??{ data_type => "INT", default_value => undef, is_nullable => 1, size => 11 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->belongs_to( parent => 'MyApp::Schema::DB::Result::Group'
=> 'parent' );
__PACKAGE__->has_many( subgroups => 'MyApp::Schema::DB::Result::Group',
?? ?{ 'foreign.parent' => 'self.id' },
?? ?{ order_by => 'name' },
?? ?);

So when there's an ID number in "parent" the current group object has
a parent group
object. When there's no ID (or it's zero) then that's the top-level group in its
hierarchy.

Creating a method for the top-level group is simple:

sub top {
??my $self = shift;
??my $top = $self;
??while ( $self = $self->parent ) {
?? ?$top = $self;
??}
??return $top;
}

If we're looking for a certain PARENT with a given ID, that's simple too:

sub specific_parent {
??my $self = shift;
??my $id ? = shift;
??while( $self->id != $id ) {
?? ?return unless $self = $self->parent;
??}
??return $self;
}

But how do we go about creating a method to look for a specific group by ID
that may not be in the parent-chain?

sub specific_group {
??my $self = shift;
??my $id ? = shift;
??return unless $self->some_manner_of_valid_id_check( $id );
??return $self->BLACK_MAGIC_HERE->find( {id=>$id} );
}

So the real question is, how do we get from $self to ->find() ?

Search Discussions

  • Tomas Doran at Sep 18, 2010 at 7:37 pm

    On 18 Sep 2010, at 17:00, will trillich wrote:

    So the real question is, how do we get from $self to ->find() ?
    From a result class, $self->resultset->find is probably what you want.

    You should also probably be asking questions about DBIx::Class on the
    DBIx::Class list, not the Catalyst list.

    Cheers
    t0m
  • Will Trillich at Sep 18, 2010 at 8:59 pm
    Thanks, Tom --

    But this apparently isn't a result-class, or I'm just missing something
    obvious:

    DB<<5>> p __PACKAGE__
    MyApp::Schema::DB::Result::Group

    DB<<7>> p "$self"
    MyApp::Model::MyApp::Group

    DB<<8>> p $self->resultset
    Can't locate object method "resultset" via package
    "MyApp::Model::MyApp::Group"

    ===

    Working from the context of a Catalyst app, I'm not sure I'd be able to
    properly abstract the right question for the DBIC list (or be able to apply
    the answer back to my concrete catalyst app). :) Folks on this list have
    been delightfully helpful despite us newbie-questions, especially you...

    Plus, being rather new to Catalyst, it's difficult to see where the edges
    are between Catalyst, DBIC and Moose.

    On Sat, Sep 18, 2010 at 3:38 PM, Tomas Doran wrote:


    On 18 Sep 2010, at 17:00, will trillich wrote:

    So the real question is, how do we get from $self to ->find() ?
    From a result class, $self->resultset->find is probably what you want.

    You should also probably be asking questions about DBIx::Class on the
    DBIx::Class list, not the Catalyst list.

    Cheers
    t0m


    _______________________________________________
    List: Catalyst@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/
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20100918/c3206058/attachment.htm
  • Steve Nolte at Sep 18, 2010 at 11:05 pm
    Agreed that this is a DBIx::Class question, but I think you want something like:

    $self->result_source->schema->resultset('NAME_OF_RESULTSET_YOU_WANT')->find($id)
  • Will Trillich at Sep 19, 2010 at 5:48 am
    Ah -- yes indeed, that did the trick. That chain was a bit longer than
    I could put together on my own.

    Thanks!

    On Sat, Sep 18, 2010 at 7:05 PM, Steve Nolte wrote:
    Agreed that this is a DBIx::Class question, but I think you want something like:

    $self->result_source->schema->resultset('NAME_OF_RESULTSET_YOU_WANT')->find($id)


    _______________________________________________
    List: Catalyst@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/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedSep 18, '10 at 4:00p
activeSep 19, '10 at 5:48a
posts5
users3
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase