[Dbix-class] Adding a method to ResultSet?
Carl Vincent
c.a.vincent at newcastle.ac.uk
Wed Jan 9 14:08:57 GMT 2008
Hello Matija,
>-----Original Message-----
>From: Matija Grabnar [mailto:matija at serverflow.com]
>Sent: 09 January 2008 13:54
>To: Class user and developer list
>Subject: [Dbix-class] Adding a method to ResultSet?
>
>(I also asked this at http://perlmonks.org/?node_id=661319 )
>
>Here is what I'm trying to do:
>
>package ThreadedDB::Article; use strict; use warnings; use base
>'DBIx::Class'; __PACKAGE__->load_components("Core");
>__PACKAGE__->table("article"); __PACKAGE__->add_columns( "id", {
>data_type => "INT", default_value => undef, is_nullable => 0,
>size => 11
>}, # etc, etc, etc ); __PACKAGE__->set_primary_key("id");
>__PACKAGE__->has_many( "article_texts", "ThreadedDB::ArticleText", {
>"foreign.article" => "self.id" }, ); # Created by
>DBIx::Class::Schema::Loader v0.04004 @ 2008-01-03 18:12:12 # DO NOT
>MODIFY THIS OR ANYTHING ABOVE! md5sum:Y0WFrRSlgOqaz/fbhRt98A package
>ThreadedDB::Article::ResultSet; use base 'DBIx::Class::ResultSet'; sub
>insert_article { my ($self, $topic, $parent, $msgtext) = @_; my
>$articles = $self->resultset('Article'); eval { $self->txn_do
>(sub { # a
>complex operation that is not relevant yet }) }; }
>
>In other words, I'd like to find a way to put a method into the
>Article.pm file in such a way that I could access it by doing
>$schema->resultset('Article')->insert_article(...)
>
>I've tried directly declaring the class (as commented out,
>above) but it
>didn't work. Either I'm misunderstanding what the resulting
>class should
>be called, or I'm misunderstanding something else.
>
>I did read Re^2: [DBIX::Class] problem with Arbitrary SQL through a
>custom ResultSource <http://perlmonks.org/?node_id=658193>, but it
>didn't help since the method I need contains a bunch of code
>(not all of
>which can be in SQL), and all of which should be done inside a
>transaction.
>
>my $articles = $schema->resultset('ThreadedDB::Article');
> DB<3> x $articles->can('create')
>0 CODE(0x88b1cd4)
> -> &DBIx::Class::ResultSet::create in
>/usr/share/perl5/DBIx/Class/ResultSet.pm:1625-1630
>
>Looks like I'm looking at the right class...
>
> DB<4> x $articles->can('insert_article')
>0 undef
>
>But it doesn't know how to "insert_article".
>
>I tried looking at the return from $schema->resultset to see
>if that would give me a clue as
>to which class I should add methods to, but that gave me:
>
>DB<5> x ref $schema->resultset('ThreadedDB::Article')
>0 'DBIx::Class::ResultSet'
>
>And I don't think I should be adding my methods to that.
>
>I also tried doing it without the extra "package" declaration (in the
>hope that it would somehow automagically do the right thing, but that
>didn't work, either.
>
>I'd be grateful for any pointers...
>
As pointed out in replies on Perlmonks - you need a Resultset. You're
problem seems to be getting that picked up correctly.
Check the "Predefined searches" part of DBIx::Class::Manual::Cookbook
for the examples of how to use
__PACKAGE__->resultset_class('My::DBIC::ResultSet')
in your result class.
Regards
Carl
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Carl Vincent http://www.netskills.ac.uk/ (URL)
Systems Manager 0191 222 5003 (voice)
Netskills, Newcastle University 0191 222 5001 (fax)
Training - Accreditation - Consultancy - Development
More information about the DBIx-Class
mailing list