srchulo
srchulo

Reputation: 5203

DBIx::Class Temporary column

I am using DBIx::Class and I have a query like this:

$groups = $c->model('DB::Project')->search(
{ "sessions.user_id"=>$c->user->id,done_yn=>'y' },
{
    select   => ["name", "id",\'SUM(UNIX_TIMESTAMP(end_time)-UNIX_TIMESTAMP(start_time)) as total_time'], #\''
    join         => 'sessions',
  }
);

I'd like to be able to get the value of SUM(UNIX_TIMESTAMP(end_time)-UNIX_TIMESTAMP(start_time)), but because this is not a real column in the table, referencing total_time for a DBIx::Class::Row object doesn't seem to work. Does anyone know how I can get these temporary columns? Thanks!

Upvotes: 3

Views: 320

Answers (1)

Alexander Hartmaier
Alexander Hartmaier

Reputation: 2204

The select docs describe perfectly how to achieve what you're trying to accomplish. It's also recommended to avoid literal SQL when possible, you can use { sum => \'UNIX_TIMESTAMP(end_time)-UNIX_TIMESTAMP(start_time)' } instead. The 'as' in the literal SQL isn't required to give the column a name, you have to use either the as search attribute or better the columns shortcut instead of select+as.

Upvotes: 2

Related Questions