Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

group_by mechanism in DB api


#1

Hi,

I’m trying to create a query using a GROUP BY using the DB api used in TestRail in order to support grouped aggregation. There doesn’t seem to be a $db->group_by() mechanism. I’ve worked around it using the following hack:

$this->db->where_expr(‘1=1) GROUP BY (tests.id’);

But that’s horribly ugly. Is there a better way to do it?


#2

Hello Chris,

Thanks for your posting. While there’s no group_by method, you can use the generic ‘query’ method:

[code]$query = $this->db->query(
‘SELECT * FROM … GROUP BY …’
$param1,
$param2);

$rows = $query->result();[/code]

Passed parameters can be accessed in the SQL via {0}, {1}, etc. and are automatically escaped and quoted if needed.

I hope this helps and please let me know in case you have any further questions.

Regards,
Tobias