Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Created January 21, 2015 11:41
Show Gist options
  • Save chanmix51/eb749f1eb47286109e3e to your computer and use it in GitHub Desktop.
Save chanmix51/eb749f1eb47286109e3e to your computer and use it in GitHub Desktop.
case when in a projection definition
<?php
// …
public function createProjection()
{
$age = <<<FIELD
case
when age(%published_at) > '1 year 1 day'::interval then date_trunc('year', age(%published_at))
when age(%published_at) > '1 month 1 day'::interval then date_trunc('month', age(%published_at))
when age(%published_at) > '1 day'::interval then date_trunc('day', age(%published_at))
when age(%published_at) > '1 hour'::interval then date_trunc('hour', age(%published_at))
else date_trunc('minutes', age(%published_at))
end
FIELD;
return parent::createProjection()
->setField('age', $age, 'interval')
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment