doll
doll copied to clipboard
Implement "count" method
This is useful when dealing with queries related to pagination where you want to retrieve data with a limit and offset, but also need to know the total number of records that match the query, e.g.
$sth = $db->prepare("SELECT `foo` FROM `bar` WHERE `baz` = 'qux' LIMIT 0, 10");
$sth->fetchAll();
The above gives up to 10 results and no indications of how many results there are in total that match the query.
It would be great to have a method count, which effectively is able to interpret the existing query, remove the "LIMIT" claus and give the number of total available records:
$sth->count();
This in effect would execute another query:
$sth = $db->prepare("SELECT COUNT(1) FROM `bar` WHERE `baz` = 'qux'");
The only drawback is that this would introduce SQL parser dependency.
- You cannot implement this feature neatly without full-featured query-builder. Yet one should be separated from DBAL - this is my strong opinion. So, if you want to use such a feature - create a query-builder based on Doll and then use its methods in your application
- There is a proprietary solution from mysql - SQL_CALC_FOUND_ROWS / FOUND_ROWS() which in theory should be faster than running the same query twice