Home » Database » DB_DataObject » Bug #4266
Allow joins with multiple keys
Details
| 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) |
Comments
[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.
[2006-04-25 15:55 UTC] mfriedman at symcor dot com
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
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-08-31 13:25 UTC] bjsimons at ufl dot edu
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-------------------------------