Re: [Catalyst] Doing work inside the DBIx::Class model

2008-02-23 Thread Matt S Trout
On Thu, Feb 21, 2008 at 09:17:33AM +0100, Peter Sørensen wrote:
> 
> >He meant result_source:
> >http://cpan.uwinnipeg.ca/htdocs/DBIx-Class/DBIx/Class/ResultSet.html#result_source
> >
> >But this is not all and it is something that begs for a FAQ entry - I always 
> >forget how to do it.  The important thing is how to add new >>>methods to 
> >the ResultSet:
> >
> >package ArchivedBooks;
> >  use base qw/DBIx::Class/;
> >  __PACKAGE__->table('books_archive');
> >  __PACKAGE__->source_name('Books');
> >  __PACKAGE__->resultset_class('ArchivedBooks::ResultSet');
> >
> >package ArchivedBooks::ResultSet;
> >use strict;
> >use warnings;
> >
> >use base qw( DBIx::Class::ResultSet );
> >
> >sub advanced_search {
> > my ( $self, $params, $attrs ) = @_;
> >
> >$self now is a ResultSet - so you can use methods on it.
> ..
> 
> 
> I have a similar problem want -  to add some custom methods to the model. 
> I've read
> your references but still - I can't get this thing to work.
> 
> I have generated the model using Catalyst and get the following layout:
> 
>   LogParser.pm
>   LogParser
>   |
>   |Components.pm
>   |Connectinfo.pm
>   |Logtype.pm
> 
> and if I take a look into Components it looks like:
> 
> package LogParser::Schema::LogParserDB::Components;
> 
> use strict;
> use warnings;
> 
> use base 'DBIx::Class';
> 
> __PACKAGE__->load_components("Core");
> __PACKAGE__->table("components");
> __PACKAGE__->add_columns(
>   "id",
>   { data_type => "INT", default_value => undef, is_nullable => 0, size => 11 
> },
>   "component",
>   { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 32 
> },
>   "regex",
>   { data_type => "TEXT", default_value => "", is_nullable => 0, size => 65535 
> },
>   "nvars",
>   { data_type => "INT", default_value => 1, is_nullable => 0, size => 10 },
>   "varnames",
>   { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 64 
> },
> );
> __PACKAGE__->set_primary_key("id");
> 
> 
> # Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-02-12 17:08:38
> # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:gkSc31rRGpYHxL8tAAd/kw
> 
> # You can replace this text with custom content, and it will be preserved on 
> regeneration
> 1;
> 
> As I understand I have to add to above:
> 
> __PACKAGE__->source_name('SomeName');
> __PACKAGE__->resultset_class('Components::ResultSet');
> LogParser::Schema::LogParserDB::Components;ame" 
> and create a new package with:
> 
> package LogParser::Schema::LogParserDB::Components::ResultSet;

load_classes() is trying to use this as a result class.

Either move it or switch to load_namespaces.

Note also there's a dbix-class list and I don't see a single bit of Catalyst
code in your post, so I'd suggest you'd be better posting there.

-- 
  Matt S Trout   Need help with your Catalyst or DBIx::Class project?
   Technical Directorhttp://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/http://www.shadowcat.co.uk/servers/

___
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/


Re: [Catalyst] Doing work inside the DBIx::Class model

2008-02-23 Thread Matt S Trout
On Thu, Feb 21, 2008 at 01:59:40PM +1000, Cian Barclay wrote:
> >On Wed, Feb 20, 2008 at 4:33 PM, Zbigniew Lukasiak <[EMAIL PROTECTED]>
> wrote:
> >
> >
> > He meant result_source:
> >
> http://cpan.uwinnipeg.ca/htdocs/DBIx-Class/DBIx/Class/ResultSet.html#result_source
> >
> >
> 
> Thanks Zbigniew and Toby, that's just what I needed to know. Is there
> a good way to call a class method and get a resultset to use?
> I'm using Widget::class_method($schema) to give it the schema
> from $self->result_source->schema. Is there a better way to do it?

Yes, don't write class methods in DBIC.

If the operation is on multiple rows, make it a resultset method.

If the operation is on a single row, make it a method on the row object.

If the operation is on a bunch of different things and neither of those
make sense, make it a method on the schema object.

-- 
  Matt S Trout   Need help with your Catalyst or DBIx::Class project?
   Technical Directorhttp://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/http://www.shadowcat.co.uk/servers/

___
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/


Re: [Catalyst] Doing work inside the DBIx::Class model

2008-02-21 Thread Zbigniew Lukasiak
On Thu, Feb 21, 2008 at 9:17 AM, Peter Sørensen <[EMAIL PROTECTED]> wrote:
>
>  >He meant result_source:
>  
> >http://cpan.uwinnipeg.ca/htdocs/DBIx-Class/DBIx/Class/ResultSet.html#result_source
>  >
>  >But this is not all and it is something that begs for a FAQ entry - I 
> always forget how to do it.  The important thing is how to add new >>>methods 
> to the ResultSet:
>  >
>  >package ArchivedBooks;
>  >  use base qw/DBIx::Class/;
>  >  __PACKAGE__->table('books_archive');
>  >  __PACKAGE__->source_name('Books');
>  >  __PACKAGE__->resultset_class('ArchivedBooks::ResultSet');
>  >
>  >package ArchivedBooks::ResultSet;
>  >use strict;
>  >use warnings;
>  >
>  >use base qw( DBIx::Class::ResultSet );
>  >
>  >sub advanced_search {
>  > my ( $self, $params, $attrs ) = @_;
>  >
>  >$self now is a ResultSet - so you can use methods on it.
>  ..
>
>
>  I have a similar problem want -  to add some custom methods to the model. 
> I've read
>  your references but still - I can't get this thing to work.
>
>  I have generated the model using Catalyst and get the following layout:
>
>   LogParser.pm
>   LogParser
>   |
>   |Components.pm
>   |Connectinfo.pm
>   |Logtype.pm
>
>  and if I take a look into Components it looks like:
>
>  package LogParser::Schema::LogParserDB::Components;
>
>  use strict;
>  use warnings;
>
>  use base 'DBIx::Class';
>
>  __PACKAGE__->load_components("Core");
>  __PACKAGE__->table("components");
>  __PACKAGE__->add_columns(
>   "id",
>   { data_type => "INT", default_value => undef, is_nullable => 0, size => 11 
> },
>   "component",
>   { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 32 
> },
>   "regex",
>   { data_type => "TEXT", default_value => "", is_nullable => 0, size => 65535 
> },
>   "nvars",
>   { data_type => "INT", default_value => 1, is_nullable => 0, size => 10 },
>   "varnames",
>   { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 64 
> },
>  );
>  __PACKAGE__->set_primary_key("id");
>
>
>  # Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-02-12 17:08:38
>  # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:gkSc31rRGpYHxL8tAAd/kw
>
>  # You can replace this text with custom content, and it will be preserved on 
> regeneration
>  1;
>
>  As I understand I have to add to above:
>
>  __PACKAGE__->source_name('SomeName');

This looks like a mistake to me - I did not mention source_name
anywhere - I don't know what this is for.

>  __PACKAGE__->resultset_class('Components::ResultSet');
>  LogParser::Schema::LogParserDB::Components;ame"
And what is this?

>  and create a new package with:
I usually just add this package to the same file as the source package
- in this case to Components.pm.

>
>  package LogParser::Schema::LogParserDB::Components::ResultSet;
>
> use strict;
>  use warnings;
>
>  use base qw( DBIx::Class::ResultSet );
>
>  sub advanced_search {
>   my ( $self, $params, $attrs ) = @_;
>  }
>  1;
>
>  So I created Components/ResultSet.pm  with this content.
>
>  But it fails with :
>
>  Couldn't instantiate component "LogParser::Model::LogParserDB", "Cannot load 
> schema class 'LogParser::Schema::LogParserDB': Can't locate object method 
> "source_name" via package 
> "LogParser::Schema::LogParserDB::Components::ResultSet" at 
> /usr/lib/perl5/site_perl/5.8.5/DBIx/Class/Schema.pm line 301.
>
>  I tried to remove the line:
>  __PACKAGE__->source_name('SomeName');
>
>  but still same problem.
>
>  As I read the docs the meaning of this is to use another name than the
>  default name so why use it???
>

see above


-- 
Zbigniew Lukasiak
http://brudnopis.blogspot.com/
___
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/


RE: [Catalyst] Doing work inside the DBIx::Class model

2008-02-21 Thread Peter Sørensen

>He meant result_source:
>http://cpan.uwinnipeg.ca/htdocs/DBIx-Class/DBIx/Class/ResultSet.html#result_source
>
>But this is not all and it is something that begs for a FAQ entry - I always 
>forget how to do it.  The important thing is how to add new >>>methods to the 
>ResultSet:
>
>package ArchivedBooks;
>  use base qw/DBIx::Class/;
>  __PACKAGE__->table('books_archive');
>  __PACKAGE__->source_name('Books');
>  __PACKAGE__->resultset_class('ArchivedBooks::ResultSet');
>
>package ArchivedBooks::ResultSet;
>use strict;
>use warnings;
>
>use base qw( DBIx::Class::ResultSet );
>
>sub advanced_search {
> my ( $self, $params, $attrs ) = @_;
>
>$self now is a ResultSet - so you can use methods on it.
..


I have a similar problem want -  to add some custom methods to the model. I've 
read
your references but still - I can't get this thing to work.

I have generated the model using Catalyst and get the following layout:

  LogParser.pm
  LogParser
  |
  |Components.pm
  |Connectinfo.pm
  |Logtype.pm

and if I take a look into Components it looks like:

package LogParser::Schema::LogParserDB::Components;

use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components("Core");
__PACKAGE__->table("components");
__PACKAGE__->add_columns(
  "id",
  { data_type => "INT", default_value => undef, is_nullable => 0, size => 11 },
  "component",
  { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 32 },
  "regex",
  { data_type => "TEXT", default_value => "", is_nullable => 0, size => 65535 },
  "nvars",
  { data_type => "INT", default_value => 1, is_nullable => 0, size => 10 },
  "varnames",
  { data_type => "VARCHAR", default_value => "", is_nullable => 0, size => 64 },
);
__PACKAGE__->set_primary_key("id");


# Created by DBIx::Class::Schema::Loader v0.04004 @ 2008-02-12 17:08:38
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:gkSc31rRGpYHxL8tAAd/kw

# You can replace this text with custom content, and it will be preserved on 
regeneration
1;

As I understand I have to add to above:

__PACKAGE__->source_name('SomeName');
__PACKAGE__->resultset_class('Components::ResultSet');
LogParser::Schema::LogParserDB::Components;ame" 
and create a new package with:

package LogParser::Schema::LogParserDB::Components::ResultSet;
use strict;
use warnings;

use base qw( DBIx::Class::ResultSet );

sub advanced_search {
 my ( $self, $params, $attrs ) = @_;
}
1;

So I created Components/ResultSet.pm  with this content.

But it fails with :

Couldn't instantiate component "LogParser::Model::LogParserDB", "Cannot load 
schema class 'LogParser::Schema::LogParserDB': Can't locate object method 
"source_name" via package 
"LogParser::Schema::LogParserDB::Components::ResultSet" at 
/usr/lib/perl5/site_perl/5.8.5/DBIx/Class/Schema.pm line 301.

I tried to remove the line:
__PACKAGE__->source_name('SomeName');

but still same problem.

As I read the docs the meaning of this is to use another name than the
default name so why use it???

Regards

Peter Sorensen/University of Southern Denmark/mail: [EMAIL PROTECTED]

___
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/


Re: [Catalyst] Doing work inside the DBIx::Class model

2008-02-20 Thread Zbigniew Lukasiak
On Thu, Feb 21, 2008 at 4:59 AM, Cian Barclay <[EMAIL PROTECTED]> wrote:
>
>
> >On Wed, Feb 20, 2008 at 4:33 PM, Zbigniew Lukasiak <[EMAIL PROTECTED]>
> wrote:
> >
> >
> > He meant result_source:
> >
> http://cpan.uwinnipeg.ca/htdocs/DBIx-Class/DBIx/Class/ResultSet.html#result_source
>  >
> >
>
> Thanks Zbigniew and Toby, that's just what I needed to know. Is there
> a good way to call a class method and get a resultset to use?
> I'm using Widget::class_method($schema) to give it the schema
>  from $self->result_source->schema. Is there a better way to do it?

Just guessing by the name - if you are using HTML::Widget - then check
HTML::FormFu - it has HTML::FormFu::Model::DBIC and it integrates with
Catalyst with Catalyst::Controller::HTML::FormFu.

Zbigniew
>
> thanks
> Cian
>
>
>
> ___
>  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/
>
>



-- 
Zbigniew Lukasiak
http://brudnopis.blogspot.com/

___
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/


Re: [Catalyst] Doing work inside the DBIx::Class model

2008-02-20 Thread Cian Barclay
>On Wed, Feb 20, 2008 at 4:33 PM, Zbigniew Lukasiak <[EMAIL PROTECTED]>
wrote:
>
>
> He meant result_source:
>
http://cpan.uwinnipeg.ca/htdocs/DBIx-Class/DBIx/Class/ResultSet.html#result_source
>
>

Thanks Zbigniew and Toby, that's just what I needed to know. Is there
a good way to call a class method and get a resultset to use?
I'm using Widget::class_method($schema) to give it the schema
from $self->result_source->schema. Is there a better way to do it?

thanks
Cian
___
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/


Re: [Catalyst] Doing work inside the DBIx::Class model

2008-02-19 Thread Zbigniew Lukasiak
On Feb 20, 2008 4:32 AM, Cian Barclay <[EMAIL PROTECTED]> wrote:
>
>
>
> On Feb 20, 2008 1:18 PM, Toby Corkindale <[EMAIL PROTECTED]> wrote:
> >
> >
> > On Wed, Feb 20, 2008 at 01:10:12PM +1000, Cian Barclay wrote:
> >
> > > which is usable both for Catalyst and something else? How do you
> > > get resultsets inside the model which could either be from the
> > > Catalyst model, or from the database connection if Catalyst wasn't
> > > being used?
> >
> >
> > I think you're looking for the $self->resultsource->schema method, inside
> your
> > Model?
> >
>
> I don't have one of those, do you mean $self->_source_handle->schema? Is
> it okay to use with the leading underscore?
>

He meant result_source:
http://cpan.uwinnipeg.ca/htdocs/DBIx-Class/DBIx/Class/ResultSet.html#result_source

But this is not all and it is something that begs for a FAQ entry - I
always forget how to do it.  The important thing is how to add new
methods to the ResultSet:

package ArchivedBooks;
  use base qw/DBIx::Class/;
  __PACKAGE__->table('books_archive');
  __PACKAGE__->source_name('Books');
  __PACKAGE__->resultset_class('ArchivedBooks::ResultSet');

package ArchivedBooks::ResultSet;
use strict;
use warnings;

use base qw( DBIx::Class::ResultSet );

sub advanced_search {
 my ( $self, $params, $attrs ) = @_;

$self now is a ResultSet - so you can use methods on it.

see 
http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/ResultSource.pm#resultset_class

At http://catalyst.perl.org/calendar/2007/16 I use that technique to
write an advanced_search method that is useful for turning web form
queries into DBIC queries.

-- 
Zbigniew Lukasiak
http://perlalchemy.blogspot.com/

___
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/


Re: [Catalyst] Doing work inside the DBIx::Class model

2008-02-19 Thread Cian Barclay
On Feb 20, 2008 1:18 PM, Toby Corkindale <[EMAIL PROTECTED]> wrote:

> On Wed, Feb 20, 2008 at 01:10:12PM +1000, Cian Barclay wrote:
> > which is usable both for Catalyst and something else? How do you
> > get resultsets inside the model which could either be from the
> > Catalyst model, or from the database connection if Catalyst wasn't
> > being used?
>
> I think you're looking for the $self->resultsource->schema method, inside
> your
> Model?
>

I don't have one of those, do you mean $self->_source_handle->schema? Is
it okay to use with the leading underscore?
___
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/


Re: [Catalyst] Doing work inside the DBIx::Class model

2008-02-19 Thread Toby Corkindale
On Wed, Feb 20, 2008 at 01:10:12PM +1000, Cian Barclay wrote:
> Hello Catalysters,
> 
> I'm using Catalyst with a DBIx::Class model generated by
> DBIx::Class::Schema::Loader.
> 
> I want to make my model do more work, rather than having my
> controllers fiddling around with model objects doing things to them.
> But I'm stuck on the bit where I want to get a resultset inside the
> model's subroutines. In a controller I use $c->model("Foo::Bar") to
> get the resultset. How can I get a resultset when I'm in the model?
> 
> I could pass $c in to the model, but then the model wouldn't be
> usable without Catalyst. I'd like to be able to use the model
> for other things, but from reading the DBIx::Class manual, it
> seems like I'd need the $schema object to get resultsets.
> 
> Since I'm stuck, I thought it would be a good time to ask for some
> help on this please. Is there a way to make a DBIx::Class model
> which is usable both for Catalyst and something else? How do you
> get resultsets inside the model which could either be from the
> Catalyst model, or from the database connection if Catalyst wasn't
> being used?

I think you're looking for the $self->resultsource->schema method, inside your
Model?

tjc.

___
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/