[CDBI] newbie: how can I put a placeholder on a query of a has_many relationship?

Bill Moseley moseley at hank.org
Fri Jun 8 01:42:15 BST 2007


On Thu, Jun 07, 2007 at 08:52:46PM +0100, Victor Churchill wrote:

> QG::ALM->has_many(readings => 'QG::Reading');
>
> 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\' );

Class::DBI::Sweet might not have active development but it will easily
add that functionality via SQL::Abstract.

Something like this, I suspect;

    $alm->readings( {
        readings => {
            between => [ $low, $high ],
        },
    });

For things more complex I'm a big fan of using set_sql().
SQL::Abstract is nice, but it turns out that writing SQL with SQL
works reasonably well.

Personally, I like to write specific methods in the model classes so
I'd probably have:

    $readings = $alm->readings_between_dates( $dt_start, $dt_end );

but with a more clever name.



-- 
Bill Moseley
moseley at hank.org




More information about the ClassDBI mailing list