2006-09-27 23:48:38 TableJoin object calls
I was searching for an elegant way to do table joins with objects that encapsulate database tables. The TableJoin object auto-populates the foreign keys. TableJoin is allowed to make direct sql calls to do its business, but it doesn't descend from the main Database object, so I'm not sure if I want to give it that kind of power without being a Database subclass, or at least going through the Database object. Haven't decided if that's overkill or just good delegation of authority.

A quick note about the select() method: The first argument is readable, but the next arguments refer to the where clause, the order, the direction and the (unused, here) limit.
( php )
  1  $phil = new Table( $database, 'phil' );
2 $author = new Table( $database, 'author' );
3 $philAuthor = new TableJoin( array( $phil, $author ) );
4
5 $works = $philAuthor->select(
6 array(
7 'phil' => array( '*' ),
8 'author' => array( 'lastName', 'firstName', 'middleName' )
9 ),
10 '', $wholeOrder, $whichway );


Another option could be something like the following, since the $databases will be the same, just let the TableJoin object create the Table objects within itself:
( php )
  1  $philAuthor = new TableJoin( $database, array( 'phil', 'author' ) );
2
3 $works = $philAuthor->select(
4 array(
5 'phil' => array( '*' ),
6 'author' => array( 'lastName', 'firstName', 'middleName' )
7 ),
8 '', $wholeOrder, $whichway );


The only reason I didn't do it this way already is because the code that uses this snippet also wants to keep using the $phil object for other nefarious purposes.

Minor aside: One thing I like about languages like Objective-C are their encouraged method naming in cases like that selection method, which would make remembering what to pass in part of knowing the method name. (Such a method doesn't exist, but is what I'd have the option of using if I made this method in Obj-C.)

( objc )
  1  works = [philAuthor select:selectionArray
2 where:whereClause
3 inOrder:wholeOrder
4 ascending:TRUE
5 withLimit:nil];


which somehow doesn't look quite as good in most languages, which don't break up the method name into multiple parts, as in this rough PHP equivalent:
( php )
  1  $works = $philAuthor->selectWhereInOrderAscendingWithLimit( … );

Leave a comment