[CDBI] Re: newbie: how can I put a placeholder on a query of a has_many relationship?
"Edward J. Sabol" <sabol-2oVx0MVsMgiP/[email protected]> Thu, 7 Jun 2007 16:19:24 -0400
| Newsgroups | gmane.comp.lang.perl.modules.class-dbi |
|---|---|
| Message-ID | <[email protected]> |
> For a given ALM I'd like to query the 'readings' relationship: my > $iterator = $ALM->readings ; The above works but gives me /all/ the > readings, ever. I'd like to query the readings between two dates. > > Now I get that I can query the readings class itself , e.g. my > $extraSQL = qq(REA_READING_DATE >= \'$to\' - ? AND REA_READING_DATE > <= \'$to\' ); > > QG::Reading->add_constructor(rdgs_past_days => $extraSQL); > > but that's a constructor on the Readings class not the ALM class with > the 'readings' "method". > > So far I can't see how to add a where clause to the 'readings' call > defined in the has_many statement. From here it looks as of, to do > what I want, I'd have to do a direct query on the Readings class, but > then I'd need to add an additional placeholder for the rea_alm_id > attribute, which kind of defeats the object of defining the has_many > and is getting close to good ole DBI. > > Some kind soul tell me what I'm missing? I don't think you're missing anything. There's no built-in way of doing this. It sounds like you want a more flexible implementation of has_many limiting: http://search.cpan.org/~tmtm/Class-DBI/lib/Class/DBI.pm#Limiting You can't really do complex limiting queries with that, however, AFAIK. You have a couple options that I can see: 1. You *might* be able to develop your own Class::DBI::Relationship subclass to implement this. Refer to HasMany.pm in the Class::DBI distribution and/or Class::DBI::Relationship::HasManyOrdered on CPAN for examples. I'm not aware of any implementations that do exactly what you want, but I agree that it would be nice to have this capability. If you go this route and get it working, please share the Relationship code with us! 2. Put the add_constructor() in the QG::Reading class and then add a simple "wrapper method" in QG::ALM which takes the number of days as an argument and passes that argument and any relevant object attributes along to the QG::Reading constructor that you created and then returns the results. I think this is what almost everyone does. It's usually only a 3-4 lines of pretty simple code to implement such a method. Hope this helps, Ed