Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.11.5

Request #4266 Allow joins with multiple keys
Submitted: 2005-05-03 06:57 UTC
From: stefan dot doeppert at synapsy dot com Assigned: alan_k
Status: Closed Package: DB_DataObject
PHP Version: 5.0.3 OS: Suse Linux 9.3
Roadmaps: (Not assigned)    
Subscription  


 [2005-05-03 06:57 UTC] stefan dot doeppert at synapsy dot com
Description: ------------ I'd like the option of allowing multiple keys to be used with the joinAdd-method. Example: links.ini: [table_a] field2 = table_c:field2 [table_b] field1 = table_a:field1 field2 = table_a:field2 This means, that the $ofield and $tfield have to be arrays. I've changed the joinAdd-method and it works fine (admitting I've only tested it with ini-files, not with arrays that display the links) Perhaps you might find it useful and integrate it in a futher release. (I'm using DB_DataObject 1.7.13) Reproduce code: --------------- I'll send the joinAdd-method with an e-mail, maybe somebody can upload it somewhere.

Comments

 [2005-05-17 23:21 UTC] alan_k
you should use diff -u to send patches that are easy to integrate. I cant remember if I actually added this one in.., or just got confused about the potential issues with it.
 [2006-04-25 15:55 UTC] mfriedman at symcor dot com (Matt Friedman)
Hi, We use Data Object extensively and have run into the same issue. Just wondering if any progress was made on this? Did Stephan ever send the patch to Alan? Right now we're using a bit of a work-around hack. It would be great to get Stephan's patch integrated. Many thanks, Matt.
 [2006-05-06 10:23 UTC] roehr at zilleon dot com (Torsten Roehr)
I have created a patched version of the joinAdd() method to support joins on multiple columns: http://www.zilleon.de/PEAR/DB_DataObject/DB_DataObject_joinAdd.php.txt The changed code can be identified by comments (/* ADDED */ and so on). The "table1, table2" syntax is not recognized yet (works in the old way), but for INNER/LEFT/RIGHT and the array syntax it should work. Best regards, Torsten Roehr
 [2006-05-06 17:28 UTC] justinpatrin (Justin Patrin)
What array syntax? That looks like a normal links.ini to me. As I said on the mailing list this breaks the current single-field link idea that DB_DataObject uses. It *could* be possible that you would want those 2 fields linked seperately. In order to support this you need to add some new syntax for multi-column links at the very least. I would suggest using a new section with a new format so that this actually works like a feature rather than a hack. Something like: [table_b__multiLinks] field1,field2 = table_a:field1,field2 And further, as I also said on the mailing list, for this to be supported properly all of DB_DataObject needs to have multi-column primary key support.
 [2006-08-31 13:25 UTC] bjsimons at ufl dot edu (Barry Simons)
I have created a patch that works for composite keys. If a single primary key is specified in the links.ini file, then the original code is executed. An array is formed and handled appropriately when a composite key is specified in the links.ini file. Example links.ini: [table1] ;works as normal key1 = table2.key1 [table3] ;works with patch key1,key2 = table4.key1,key2 This patch works for me. Please try it and let me know if there are any problems. You can patch your existing DataObject.php by placing the segment between the begin and end patch statement in a separate file (DataObject.php.patch), place it in the same directory as DataObject.php, and then issue patch -p0 < DataObject.php.patch to patch the file. The patch file is distorted in this post because of line wraps. Make sure that you restore the longer lines back to one line. For example, /* link contains {this column} = {linked table}:{linked column} */ should be one line. Barry --------------------BEGIN PATCH---------------------------------- --- DataObject.orginal.php 2006-08-31 08:16:17.000000000 -0400 +++ DataObject.php 2006-08-31 08:16:40.000000000 -0400 @@ -3079,6 +3079,18 @@ foreach ($links as $k => $v) { /* link contains {this column} = {linked table}:{linked column} */ $ar = explode(':', $v); + + $links_key_array = strpos($k,','); + if ($links_key_array !== false) { + $k = explode(',', $k); + } + + $ar_array = strpos($ar[1],','); + if ($ar_array !== false) { + $ar[1] = explode(',', $ar[1]); + } + + if ($ar[0] == $obj->__table) { if ($joinCol !== false) { if ($k == $joinCol) { @@ -3187,8 +3199,21 @@ case 'INNER': case 'LEFT': case 'RIGHT': // others??? .. cross, left outer, right outer, natural..? - $this->_join .= "\n {$joinType} JOIN {$objTable} {$fullJoinAs}". - " ON {$joinAs}.{$ofield}={$table}.{$tfield} {$appendJoin} "; + $this->_join .= "\n {$joinType} JOIN {$objTable} {$fullJoinAs}"; + if (is_array($ofield)) { + $key_count = count($ofield); + for($i = 0; $i < $key_count; $i++) { + if ($i == 0) { + $this->_join .= " ON {$joinAs}.{$ofield[$i]}={$table}.{$tfield[$i]} {$appendJoin} "; + } + else { + $this->_join .= " AND {$joinAs}.{$ofield[$i]}={$table}.{$tfield[$i]} {$appendJoin} "; + } + } + } else { + $this->_join .= " ON {$joinAs}.{$ofield}={$table}.{$tfield} {$appendJoin} "; + } + break; case '': // this is just a standard multitable select.. $this->_join .= "\n , {$objTable} {$fullJoinAs} {$appendJoin}"; ----------------------END PATCH-------------------------------
 [2006-11-11 04:07 UTC] alan_k (Alan Knowles)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better. If you could patch the documentation - that would be nice ;) Thanks Alan
 [2007-01-03 15:45 UTC] troehr (Torsten Roehr)
First of all a big thank you for incorporating this long-awaited feature. There's still a small bug, though. The append join string must be appended AFTER all ON columns have been concatenated. A patch fixing this against current CVS can be found here: http://phpfi.com/190241 Thanks and best regards, Torsten Roehr
 [2007-01-03 16:06 UTC] troehr (Torsten Roehr)
I have also created a patch to include this feature into the end-user documentation: http://phpfi.com/190255 For: http://cvs.php.net/viewvc.cgi/peardoc/en/package/database/db-dataobject/intro-relations.xml
 [2007-01-09 05:35 UTC] alan_k (Alan Knowles)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.
 [2007-02-19 16:03 UTC] vv43 (VV43)
The bug still remains if you don't use the links() function of the object. To solve the problem you have to replace lines 3129 and 3130 : - $tfield = $k; + $tfield = explode(",", $k); - $ofield = $ar[1]; + $ofield = explode(",", $ar[1]);
 [2007-02-19 16:13 UTC] vv43 (vv43)
Here is the exact patch --- DataObject.php 2007-02-19 20:07:01.000000000 +0100 +++ DataObject2.php 2007-02-19 20:06:43.000000000 +0100 @@ -3111,7 +3111,16 @@ foreach ($links as $k => $v) { /* link contains {this column} = {linked table}:{linked column} */ $ar = explode(':', $v); - + // Feature Request #4266 - Allow joins with multiple keys + $links_key_array = strpos($k,','); + if ($links_key_array !== false) { + $k = explode(',', $k); + } + $ar_array = strpos($ar[1],','); + if ($ar_array !== false) { + $ar[1] = explode(',', $ar[1]); + } + if ($ar[0] == $obj->__table) { if ($joinCol !== false) { if ($k == $joinCol) {
 [2007-02-21 01:42 UTC] alan_k (Alan Knowles)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better. Thanks = let me know if there's still any issues.