[Dbix-class] Subselect, max, now()

Paul Makepeace paulm at paulm.com
Wed Sep 19 00:32:10 GMT 2007


OK, I give up. Could someone please help convert this SQL into DBIx::Class,

  select story.date_time, story.source_uid, story.headline
  from story where story.publication_uid = 23 and date_time = (
    select max(story.date_time)
    from story join publication on story.publication_uid = publication.uid
    where publication.uid = 23 and story.date_time < now());

I sorta managed the subselect but couldn't get it to treat now() as a function,

my @SEARCH_MAX_DATE_TIME_ARGS = (
	join => [qw/story_publication/],
	select => [\'max(me.date_time)'],
	as => [qw/max_date_time/],
);

sub search_max_date_time {
	my ($obj, $search, $args) = @_; $args ||= {};
	$obj->search($search,	{ @SEARCH_MAX_DATE_TIME_ARGS, %$args });
}

Controller:
  my ($max_date_time) :Stashed =
$ds_ro->resultset('Story')->search_max_date_time(
  	{ %$search, 'me.date_time' => { '<' =>  \'now()' }})->next;
  if ($max_date_time) {
	$max_date_time = $max_date_time->get_column('max_date_time');
  }

Thanks! Other random style comments welcome.

P



More information about the DBIx-Class mailing list