[Dbix-class] can we store the DBIx::Class into cache system?

2009-07-24 Thread Mike.G
Hi, all, I have a question about the how to save the DBIx::Class object into

cache system, example: we use CHI,   my question is: just save DBIx::Class
into Cache system like other
data type? I mean just like array, hash, scalar? don't need do other things?

so,  the DBIx::Class will be lost database connection and lost data?

thanks

Mike.G
___
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk

RE: [Dbix-class] Unknown column in 'group statement'

2009-07-24 Thread Anthony Gladdish
Hi Peter,

-Original Message-
From: Peter Rabbitson [mailto:rabbit+d...@rabbit.us]
Sent: 23 July 2009 22:45
To: DBIx::Class user and developer list
Subject: Re: [Dbix-class] Unknown column in 'group statement'

On Thu, Jul 23, 2009 at 04:19:01PM +0100, Anthony Gladdish wrote:
 Hi,

 Using:
 Perl 5.10.
 DBIC 0.08108.

 1. My result source tables with relationships are:

 Event.pm:
 __PACKAGE__-set_primary_key('id');
 __PACKAGE__-belongs_to( 'leader' = 'Trainer' ) ;
 __PACKAGE__-has_many( 'assistingtrainers' = 'AssistingTrainer', 'event' );
 __PACKAGE__-many_to_many('assistants' = 'assistingtrainers', 'trainers' );

 AssistingTrainer.pm:
 __PACKAGE__-set_primary_key('event','trainer');
 __PACKAGE__-belongs_to( 'events' = 'Event', 'event' );
 __PACKAGE__-belongs_to( 'trainers' = 'Trainer', 'trainer' );

 Trainer.pm:
 __PACKAGE__-set_primary_key('id');

 2. My ResultSet::Event.pm methods:

 sub goEvents {
  my $self  = shift;
  my $where = {};
  $where-{status} = { '' = 2 };
  return $self-search($where);
 }

 sub events_during_lookback_period {
  my $self  = shift;
  my $default_lookback_period = DateTime::Duration-new( years = 3 );
  my $lookback_period = shift || $default_lookback_period;
  my $earliest_date = DateTime-now() - $lookback_period;
  return $self-goEvents()-search_rs(
  {
  -and = [
  start = { '', DateTime-now() },
  start = { '', $earliest_date },
  ],
  }
  );
 }

 sub distinct_assistants {
  my $self  = shift;
  my $assistants = [];
  my $rs = $self-search_rs(
  undef,
  {
  columns = [ qw/me.id/ ],
  distinct = 1,
  prefetch = [ { 'assistingtrainers' = 'trainers' } ],
  join = [ { 'assistingtrainers' = 'trainers' } ],
  },
  );
  while ( my $e = $rs-next() ) {
  foreach my $at ( $e-assistingtrainers() ) {
  push( @$assistants, $at-trainers );
  }
  }
  return $assistants;
 }


This is a design lapse - we married distinct with group_by too early
in the code and what you are seeing is the fallout (i.e. distinct
applies to all columns not just the ones you are trying to select).
I will have to discuss this with the rest of the developers before
we solve it, but for the time being simply change

distinct = 1

to

group_by = [ qw/me.id/ ]

The prefetch will keep working as expected.

Cheers

This works, but only if I omit columns = too, i.e.:

{
group_by = [ qw/me.id/ ],
prefetch = [ { 'assistingtrainers' = 'trainers' } ],
join = [ { 'assistingtrainers' = 'trainers' } ],
},

I'm assuming this is ok, although I would have thought columns and group_by 
would be needed together (correct me if I'm wrong), and doing this:

{
columns = [ qw/me.id/ ],
group_by = [ qw/me.id/ ],
prefetch = [ { 'assistingtrainers' = 'trainers' } ],
join = [ { 'assistingtrainers' = 'trainers' } ],
},


... produces error:

DBIx::Class::ResultSet::next(): DBI Exception: DBD::mysql::st execute failed: 
Unknown column 'start' in 'where clause' [for Statement SELECT me.id, 
assistingtrainers.event, assistingtrainers.trainer, trainers.id, trainers.name, 
trainers.email, trainers.phone, trainers.initials, trainers.loginId, 
trainers.password FROM (SELECT me.id FROM event me WHERE ( ( ( start  ? AND 
start  ? ) AND status  ? ) ) GROUP BY me.id) me LEFT JOIN trainer_assist 
assistingtrainers ON assistingtrainers.event = me.id LEFT JOIN trainer trainers 
ON trainers.id = assistingtrainers.trainer WHERE ( ( ( start  ? AND start  ? 
) AND status  ? ) ) ORDER BY assistingtrainers.event with ParamValues: 
0='2009-07-24T09:46:14', 1='2006-07-24T09:46:14', 2=2, 3='2009-07-24T09:46:14', 
4='2006-07-24T09:46:14', 5=2]

Thanks for your help,

Anthony

___
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk


Re: [Dbix-class] Relations for multi-class object inflation from one table

2009-07-24 Thread Wallace Reis
On Thu, Jul 23, 2009 at 4:12 PM, Oleg Kostyukcub.ua...@gmail.com wrote:
 Hello all,

 I read about Dynamic Sub-classing DBIx::Class proxy classes in
 DBIx::Class::Manual::Cookbook, and have question.

 package My::Schema::Result::Company;
 use base qw/DBIx::Class/;
 __PACKAGE__-load_components(qw/Core/);
 __PACKAGE__-table('companies');
 __PACKAGE__-add_columns(qw/company_id ../);
 __PACKAGE__-set_primary_key('company_id');
 __PACKAGE__-has_many('admins', 'My::Schema::Result::User::Admin', );
 __PACKAGE__-has_many('users', 'My::Schema::Result::User', );
 __PACKAGE__-has_many('all_users', 'My::Schema::Result::User', );

 My questions is:

 1) what should be instead of '' in has_many() calls above? I
 think, this should be something like this (for first has_many): {
 'foreign.company_id' = 'self.company_id', 'foreign.admin' = 1 } - is
 this correct?

You can't define rels using constants in FK definitions like
'foreign.admin' = 1. You have to define just the 'all_users' rel with
{ 'foreign.company_id' = 'self.company_id' } and create result class
methods for 'users' and 'admins' like:
sub users_rs {
my $self = shift;
return $self-all_users_rs({ admin = '0' });
}

sub users {
my $self = shift;
my $rs = $self-users_rs;
return wantarray ? $rs-all : $rs;
}

sub admins_rs {
my $self = shift;
return $self-all_users_rs({ admin = '1' });
}

sub admins {
my $self = shift;
my $rs = $self-admins_rs;
return wantarray ? $rs-all : $rs;
}

Or you can create a base result class called Person, then Admin and
User classes as a views (CREATE VIEW) over Person, thus you can define
the rels like:
__PACKAGE__-has_many('admins', 'My::Schema::Result::User::Admin',
{ 'foreign.company_id' = 'self.company_id' });
__PACKAGE__-has_many('users', 'My::Schema::Result::User',
{ 'foreign.company_id' = 'self.company_id' });
__PACKAGE__-has_many('all_users', 'My::Schema::Result::Person',
{ 'foreign.company_id' = 'self.company_id' });

 3) do I need write belongs_to(company) in User::Admin too, or only in
 User will be enough?

Just in User should be enough.


-- 
 wallace reis/wreis Catalyst and DBIx::Class consultancy with a clue
 Software Developer and a commit bit: 
http://shadowcat.co.uk/catalyst/
 Shadowcat Systems Limited
 http://www.shadowcat.co.uk http://www.linkedin.com/in/wallacereis

___
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk


Re: [Dbix-class] Unknown column in 'group statement'

2009-07-24 Thread Peter Rabbitson
On Fri, Jul 24, 2009 at 11:00:50AM +0100, Anthony Gladdish wrote:
 Hi Peter,
 
 -Original Message-
 From: Peter Rabbitson [mailto:rabbit+d...@rabbit.us]
 Sent: 23 July 2009 22:45
 To: DBIx::Class user and developer list
 Subject: Re: [Dbix-class] Unknown column in 'group statement'
 
 On Thu, Jul 23, 2009 at 04:19:01PM +0100, Anthony Gladdish wrote:
  Hi,
 
  Using:
  Perl 5.10.
  DBIC 0.08108.
 
  1. My result source tables with relationships are:
 
  Event.pm:
  __PACKAGE__-set_primary_key('id');
  __PACKAGE__-belongs_to( 'leader' = 'Trainer' ) ;
  __PACKAGE__-has_many( 'assistingtrainers' = 'AssistingTrainer', 'event' 
  );
  __PACKAGE__-many_to_many('assistants' = 'assistingtrainers', 'trainers' 
  );
 
  AssistingTrainer.pm:
  __PACKAGE__-set_primary_key('event','trainer');
  __PACKAGE__-belongs_to( 'events' = 'Event', 'event' );
  __PACKAGE__-belongs_to( 'trainers' = 'Trainer', 'trainer' );
 
  Trainer.pm:
  __PACKAGE__-set_primary_key('id');
 
  2. My ResultSet::Event.pm methods:
 
  sub goEvents {
 my $self  = shift;
 my $where = {};
 $where-{status} = { '' = 2 };
 return $self-search($where);
  }
 
  sub events_during_lookback_period {
 my $self  = shift;
 my $default_lookback_period = DateTime::Duration-new( years = 3 );
 my $lookback_period = shift || $default_lookback_period;
 my $earliest_date = DateTime-now() - $lookback_period;
 return $self-goEvents()-search_rs(
 {
 -and = [
 start = { '', DateTime-now() },
 start = { '', $earliest_date },
 ],
 }
 );
  }
 
  sub distinct_assistants {
 my $self  = shift;
 my $assistants = [];
 my $rs = $self-search_rs(
 undef,
 {
 columns = [ qw/me.id/ ],
 distinct = 1,
 prefetch = [ { 'assistingtrainers' = 'trainers' } ],
 join = [ { 'assistingtrainers' = 'trainers' } ],
 },
 );
 while ( my $e = $rs-next() ) {
 foreach my $at ( $e-assistingtrainers() ) {
 push( @$assistants, $at-trainers );
 }
 }
 return $assistants;
  }
 
 
 This is a design lapse - we married distinct with group_by too early
 in the code and what you are seeing is the fallout (i.e. distinct
 applies to all columns not just the ones you are trying to select).
 I will have to discuss this with the rest of the developers before
 we solve it, but for the time being simply change
 
 distinct = 1
 
 to
 
 group_by = [ qw/me.id/ ]
 
 The prefetch will keep working as expected.
 
 Cheers
 
 This works, but only if I omit columns = too, i.e.:
 
   {
   group_by = [ qw/me.id/ ],
   prefetch = [ { 'assistingtrainers' = 'trainers' } ],
   join = [ { 'assistingtrainers' = 'trainers' } ],
   },
 
 I'm assuming this is ok, although I would have thought columns and 
 group_by would be needed together (correct me if I'm wrong), and doing this:
 
   {
   columns = [ qw/me.id/ ],
   group_by = [ qw/me.id/ ],
   prefetch = [ { 'assistingtrainers' = 'trainers' } ],
   join = [ { 'assistingtrainers' = 'trainers' } ],
   },
 
 
 ... produces error:
 
 DBIx::Class::ResultSet::next(): DBI Exception: DBD::mysql::st execute failed: 
 Unknown column 'start' in 'where clause' [for Statement SELECT me.id, 
 assistingtrainers.event, assistingtrainers.trainer, trainers.id, 
 trainers.name, trainers.email, trainers.phone, trainers.initials, 
 trainers.loginId, trainers.password FROM (SELECT me.id FROM event me WHERE ( 
 ( ( start  ? AND start  ? ) AND status  ? ) ) GROUP BY me.id) me LEFT JOIN 
 trainer_assist assistingtrainers ON assistingtrainers.event = me.id LEFT JOIN 
 trainer trainers ON trainers.id = assistingtrainers.trainer WHERE ( ( ( start 
  ? AND start  ? ) AND status  ? ) ) ORDER BY assistingtrainers.event with 
 ParamValues: 0='2009-07-24T09:46:14', 1='2006-07-24T09:46:14', 2=2, 
 3='2009-07-24T09:46:14', 4='2006-07-24T09:46:14', 5=2]
 

This unfortunately is a limitation which can not be worked around easily.
The problem is simple - there is no usable introspection of the WHERE
condition, thus there is no sane way to tell *what* the WHERE limits
on. In your case WHERE is necessary only on the inner query, as it works
on me.* columns only. However since I have no reliable way of determining
this, I err on the safe side, and include the WHERE in the inner and
outer query[1]. The right way to solve this is to bug ash to finish
SQLA2 :) However in the meantime you can trick this with distinct as
such:

{
select = [ { distinct = 'me.id' } ],
as = [qw/id/],
prefetch = [ { 'assistingtrainers' = 'trainers' } ],
join = [ { 'assistingtrainers' = 'trainers' } ],
},

HTH

[1] The reason the double WHERE is