In an application I implemented a Mapper to retrieve data from the database and objects from the ResultSet. In order to get all the data I have to JOIN multiple tables. Then I get a huge array structured like this:
[
0 => [
main_id => 1,
main_title => 'lorem',
main_desc => 'ipsum',
foo_id => '12',
foo_name => 'qwer',
bar_id => '56',
bar_name => 'asdf'
],
1 => [
main_id => 1,
main_title => 'lorem',
main_desc => 'ipsum',
foo_id => '12',
foo_name => 'qwer',
bar_id => '67',
bar_name => 'hjkl'
],
2 => [
main_id => 1,
main_title => 'lorem',
main_desc => 'ipsum',
foo_id => '23',
foo_name => 'uiop',
bar_id => '67',
bar_name => 'hjkl'
],
...
10 => [
main_id => 1,
main_title => 'lorem',
main_desc => 'ipsum',
foo_id => '23',
foo_name => 'uiop',
bar_id => '91',
bar_name => 'cvbn'
],
11 => [
main_id => 2,
main_title => 'dolor',
main_desc => 'sit',
foo_id => '78',
foo_name => 'fghj',
bar_id => '89',
bar_name => 'vbnm'
],
...
12 => [
main_id => 3,
foo_id => '135',
bar_id => '246',
...
],
13 => [
main_id => 3,
foo_id => '135',
bar_id => '468',
...
],
14 => [
main_id => 3,
foo_id => '357',
bar_id => '680',
...
],
...
1000 => [
...
]
]
Then I iterate over the array, build objects (Main, Foo, Bar etc.), and combine them, e.g.
$newMain = $myMainHydrator->hydrate($results[0]);
$newFoo = $myFooHydrator->hydrate($results[0]);
$newBar = $myBarHydrator->hydrate($results[0]);
$newFoo->setBar($newBar);
$newMain->setFoo($newFoo);
$resultObjects[] = $newMain;
Now I built in a Paginator and got following issue: The Paginator sets a LIMIT, e.g. 10, and retrieves then only 10 rows, while I need for every object more than one result row (currently even 12 rows).
I cannot believe, that the Paginator cannot handle JOINs, so there must be a way to get it working. How to use the Paginator for complex SELECTs with JOINs?
The issue can be solved by replacing of the
LIMITclause by anINin thePaginatorAdapter'sgetItems(...).As you see in this case the
IDs first have to be retrieved with a separate database request. But anyway -- it works. If you know a better solution, please feel free to post it.