[eluser]stensi[/eluser]
At the moment, I've changed DataMapper's find() method to accept another two optional parameters, those being <b>limit</b> and <b>offset</b>. This is useful for paging your results. Example usages:
Code:
// Get all books from year 2000
$book = new Book(array('year' => 2000));
// Get all books from year 2000, limiting to 20 results
$book = new Book(array('year' => 2000), 20);
// Get all books from year 2000, limiting to 20 results,
// with an offset of 40 to view what would be the 3rd page of results
$book = new Book(array('year' => 2000), 20, 40);
Alternatively, I'm setting it up so you can do it this way:
Code:
$book = new Book();
$b->year = 2000;
$b->limit = 20;
$b->offset = 40;
$b->find();
Which gives the same result. You'll be able to setup a default limit in your DataMapper models, and offset will always default to 0 unless you specify it. After each find() call it will reset to 0 again.
<b>Note:</b>This newer version hasn't been uploaded yet as I'm hoping to finish off a few other additions first, including some cleaner handling of validation errors.
I'm having some problems coming up with a good naming convention for putting in the rest of the CodeIgniter DB functionality, such as ordering, grouping and like queries, into DataMapper. Obviously it's a little difficult to cram it all into the find() method and still keep it useable. Here's some samples of what I was thinking of doing:
Code:
// This will produce results ordered by the book name in ascending order, grouped by year.
$book = new Book();
$book->order = array('name', 'asc');
$book->group = array('year');
$book->find(array('year >' => 1990), 20);
I'm not too happy with having it so find() has a whole mess of optional parameters, such as:
Code:
$object->find($condition, $limit, $offset, $order, $group);
But if that's what you'd prefer instead of, or in addition to the example above it, I can do both or either.
It's likely that I'll end up having to add some other methods such as search() or find_like() for like queries but I'm hoping to get suggestions on the naming and usability.
Then there's select_max, select_min, select_avg etc that I was looking to add in. I've pretty much decided on those though. They'll look something like:
Code:
// Get the history genre
$genre = new Genre(array('name' => 'history'));
// Get the oldest year of this genres books
$oldest_year = $genre->book->min('year');
// Get the newest year of this genres books
$newest_year = $genre->book->max('year');
// Get the average year of this genres books
$average_year = $genre->book->avg('year');
// Get the total cost of buying all books from this genre
$total_cost = $genre->book->sum('price');
So yes, any suggestions on the naming/usage for ordering, grouping, and like queries and anything else are very welcome! :-)
<b>UPDATE</b>
The above is no longer valid as of version 1.2.