views:

8

answers:

1

Hi,

I have two database tables (lets call them A and B) with many-to-many bridge table (AB). I have written Zend_Db_Table models for all three tables.

I have an A_Row object I extracted from the database using for example Zend_Db_Row::find method. Now I need get print a table of all rows, which are in relationship with this row. The problem is I need to have this table paginated, but the table is rendered using a method of B_Rowset.

I can see two options (if I want to use Zend_Paginator)

1) Use Zend_Paginator_Adapter_Iterator to paginate the rowset returned by findManyToManyRowset. This would look like this:

$b_rowset = $a_row->findManyToManyRowset('B_Table', 'AB_Table');
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Iterator($b_rowset));

However, there are two problems:

a) Whole result set (not only items on the page) is fetched.

b) I use a method of B_Rowset to render the table HTML, so I would have to convert the results back into result set.

This solution seems a little too ugly.

2) Build the Zend_Db_Table_Select manually and use Zend_Paginator_Adapter_DbTableSelect.

But building multi-table queries with Zend_Db_Table_Select is very unpretty (and the results need to be converted into B_Rowset). I would likely end up with 50 or so lines of messy code.

I could use either method (and I would likely be done faster than writing this post), but I want to see some opinions, how to solve this nicely (since I expect to encounter this scenario again and again).

A: 

I have tried both of these, or at least 2, I did have a solution that was similar to 1 which used findDependentRowset().

One. Worked well. It was harder to set up and get working, but it means that the queries are built for me with less hassle when it comes to implementation.

Two. I ended up using this method however. It allows for greater flexibility and allows you to join tables only when you need them joined. This works well with Zend pagination too. My solution was to write functions within my model to which I can pass a Zend_Db_Select object to, the function in the model would then apply a join to the Zend_Db_Select object, similar to this.

 // In Model A, you would create a function to join table B
 // this is much simpler, but hopefully you get the idea
 function joinB(Zend_Db_Select &$select)
 {
      $select->join(...);
      return $this;
 }

This is of course not the solution for everyone, but it provides me with what I need.

I wrote 'one' and 'two' earlier because the proper list syntax seemed to be messing up the code highlighting

jakenoble