View Full Version : Manage relational tables with two key fields
this_is_me
06-01-2009, 09:18 PM
I ahve put a similar question before, but here it seems even more relevant: I have a relational table, consisting of only two fields:
foreign key a and foreign key b pointing to table products (key a) and categories (key b).
In my case I setup the structure so that both fields form the key. I attach a phpmyadmin screenshot. How can I use this with MRT?
Russell Collins
06-02-2009, 12:44 AM
Hi,
I noticed that you made them both primary keys. I don't think that'll work. Because your PK's are in the other 2 tables.
If you remove both primary keys, click on the relation view icon just above the index area and use the "link to" and link them to your PK's in the other tables you'll have just index keys on your 2 table items.
I think you need to make the tables innoDB as well, but not sure about that.
Regards
Russ
Dave Buchholz
06-02-2009, 01:09 AM
On your update products page where I assume you will be using the MRT SB you would pass the product id using the id value returned in your recordset and then pass the value for categories from a select list, here is some code from a previous project that used this logic:
<?php
// WA DataAssist Manage Relational Table 1
if (isset($_POST["update_x"])) // Trigger
{
$WA_arrayOrFieldList = 0;
$WA_joinLoopedField = "assocCat";
$WA_loopedField = "";
$WA_connection = $connDB;
$WA_table = "lnk_product_category";
$WA_redirectURL = "view-product.php?prodID=".((isset($_POST["prodID"]))?$_POST["prodID"]:"") ."";
$WA_keepQueryString = false;
$WA_masterKeyField = "prodID";
$WA_masterKeyType = "none,none,NULL";
$WA_masterKeyValue = "".$row_rsProducts['prodID'] ."";
$WA_masterKeyComp = "=";
$WA_joinedKeyField = "catID";
$WA_joinedKeyType = "none,none,NULL";
$WA_joinedKeyComp = "=";
$WA_fieldNamesStr = "prodID|catID";
$WA_columnTypesStr = "none,none,NULL|none,none,NULL";
$WA_appliedString = "";
$WA_appliedList = array();
$WA_valuesList = array();
$WA_connectionDB = $database_connDB;
mysql_select_db($WA_connectionDB, $WA_connection);
if ($WA_arrayOrFieldList == 0) {
$WA_loopedValues = $_POST[$WA_joinLoopedField];
for ($v=0; $v<sizeof($WA_loopedValues); $v++) {
$WA_fieldValues = "^MASTERID^" . "|" . "^JOINID^";
$WA_valuesList[] = array($WA_loopedValues[$v], $WA_fieldValues);
$WA_appliedString .= "^" . $WA_loopedValues[$v] . "^";
$WA_appliedList[] = $WA_loopedValues[$v];
}
}
else if ($WA_arrayOrFieldList == 1) {
$WA_mrtLoopCounter = 0;
while (WA_AB_checkLoopedFieldsExist(array($WA_loopedFiel d), $WA_mrtLoopCounter)) {
if (WA_AB_checkLoopedFieldsNotBlank(array($WA_joinLoo pedField), $WA_mrtLoopCounter) && WA_AB_getLoopedFieldValue($WA_loopedField, $WA_mrtLoopCounter) == WA_AB_getLoopedFieldValue($WA_joinLoopedField, $WA_mrtLoopCounter)) {
$WA_fieldValues = "^MASTERID^" . "|" . "^JOINID^";
$WA_valuesList[] = array(WA_AB_getLoopedFieldValue($WA_loopedField, $WA_mrtLoopCounter), $WA_fieldValues);
$WA_appliedString .= "^" . WA_AB_getLoopedFieldValue($WA_loopedField, $WA_mrtLoopCounter) . "^";
$WA_appliedList[] = WA_AB_getLoopedFieldValue($WA_loopedField, $WA_mrtLoopCounter);
}
$WA_mrtLoopCounter++;
}
}
WA_AB_doManageRelationalTable($WA_valuesList, $WA_appliedString, $WA_appliedList, $WA_connection, $WA_table, $WA_masterKeyField, $WA_masterKeyType, $WA_masterKeyValue, $WA_masterKeyComp, $WA_joinedKeyField, $WA_joinedKeyType, $WA_joinedKeyComp, $WA_fieldNamesStr, $WA_columnTypesStr);
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>
A couple of explanations might be in order
$WA_joinLoopedField = "assocCat"; is the select list containing the categories
$WA_table = "lnk_product_category"; is my database table set up like yours (see attached screenshot)
$WA_masterKeyValue = "".$row_rsProducts['prodID'] .""; is the product ID recordset value
you should be able to work out everything else from there.
this_is_me
06-19-2009, 01:53 AM
Hi,
I noticed that you made them both primary keys. I don't think that'll work. Because your PK's are in the other 2 tables.
If you remove both primary keys, click on the relation view icon just above the index area and use the "link to" and link them to your PK's in the other tables you'll have just index keys on your 2 table items.
I think you need to make the tables innoDB as well, but not sure about that.
Regards
Russ
Thank you for your reply. You made good points.
But my idea was:
It is necessary to have them both defined (together) as primary keys because only each pair must be unique. I wonder if this option will be implemented in DA one day? I learned this trick from osCommerce which is an open source webshop soulution with some real good options.
Ray Borduin
06-19-2009, 07:00 AM
I don't see why it wouldn't work. Where are you getting stuck?
ken413883
10-15-2010, 09:45 PM
I have a table that has 3 fields, 2 of which are FK's to other tables and those to FK's as a pair must be unique, so they are both set to primary key...which works great in all other parts of the site.
However, I am trying to now set up and update screen for the 3rd field, and I can't figure out how to pass both primary keys as variables to the update page.
Is there a way to force DA to use 2 fields as primary key? If not, what code do I enter/modify on the update page (which is all I really need) to pull the correct result?
Example:
People = FK to People Table - PK
Profession = FK to Profession Table - PK
Salary = requires update fields
A single person may have multiple professions, and each profession has a different salary range associated with it - by the same token, multiple people could have the same professions, and have different salary range(s) than other folks in that profession...
When someone clicks the link on the Person Detail page (which will point to the update salary page for that profession) I need to make sure it pulls the correct person and the correct profession so they can edit the appropriate salary line...
I don't really have any code to attach, because I can't figure out how to complete the DA Wizard with 2 primary keys...
Help?
Ray Borduin
10-18-2010, 06:15 AM
If you are using mySQL 5+ you could create a view that concatinates the two keys into a single unique key. DataAssist is designed to only work with a single key, but by concatinating the two keys you should be able to create a single unique key.
vBulletin® v3.8.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.