TomWitt
Založený: 30.08.2004
Príspevky: 5
Zaslal: 2007-05-04 17:31
Návrat hore
Odpovedať s citátom
|
I've been struggling to figure out how virtual fields/columns and joins work. I finally found a solution which solves a problem I'd had. I'm sharing it here knowing others will have the same needs and questions.
Background: I wanted to have one table include multiple virtual columns from another table.
A quick example might better show what I'm talking about.
Table:
Suppliers
-- People who provide me services/stuff
- supplier_id (key field for suppliers)
- supplier_name
- supplier_address
- esupplier_phone
Table:
Invoices
-- Invoices received I need to pay
- invoice_id (key field for invoices)
- invoice_date
- supplier_id (link field to suppliers)
- invoice_amount
- payment_terms
I wanted a phpMyEdit form which listed the invoices to be paid but would also display the Supplier name, address and phone number with each invoice based on supplier_id. So that would be 3 virtual columns (for supplier name, address and phone).
JammerPro came up with a pretty good solution (
http://opensource.platon.sk/forum/projects/viewtopic.php?t=961
) but it's limited to showing only one virtual column from a secondary join table using a given column in the main table as the link value.
I stumbled across (what I think is) an undocumented feature which allows you to reference a given column multiple times by appending "|<some unique number>" to the column name.
Using this feature, I was then able to setup multiple references from the supplier_id column in the main table to the virtual field columns in the join table, something like this:
$opts['fdd']['supplier_id'] = array(...); // show supplier id
$opts['fdd']['supplier_id|1'] = array(...); // virtual field one for supplier name
$opts['fdd']['supplier_id|2'] = array(...); // virtual field two for supplier address
$opts['fdd']['supplier_id|3'] = array(...); // virtual field three for supplier phone
Here's a code sample which details the solution
(Many thanks to Doug Hockinson for his wonderful examples showing how joins are used and how the (as yet) undocumented input => 'V' feature works.)
| Kód: |
// CREATE TABLE `invoice` (
// `invoice_id` INT NOT NULL auto_increment,
// `invoice_date` DATE NOT NULL ,
// `supplier_id` INT NOT NULL ,
// `invoice_amount` FLOAT NOT NULL ,
// `payment_terms` ENUM( '10 days', '30 days', '45 days', '60 days' ) NULL,
// PRIMARY KEY (`invoice_id`)
// ) ENGINE = MYISAM COMMENT = 'Invoices received that need to be paid';
// CREATE TABLE `supplier` (
// `supplier_id` int(11) NOT NULL auto_increment,
// `supplier_name` varchar(30) NOT NULL,
// `supplier_address` varchar(50) NOT NULL,
// `supplier_phone` varchar(20) NOT NULL,
// PRIMARY KEY (`supplier_id`)
// ) ENGINE=MyISAM COMMENT='People/companies who provide stuff to us';
//====================================================
// Multiple Virtual Fields/Columns example
// ------------------------------------------------
// This example shows how a non-key field in the
// main table can be used to reference a foreign key
// in a secondary table and display multiple columns
// from the secondary table as virtual fields/columns
//
// The magic is in the '|<number>' appended to the
// field name of the link field within the main table
//
// I have no idea if this is supposed to work this way
// ... but it does
//====================================================
$main_table = 'invoice';
$join_table = 'supplier';
//====================================================
// Non-key Column in main table - Supplier ID
// ------------------------------------------------
// This is the field we will use as the link from the
// main table to the key field in the join table
//
// Included here to show that the link field itself
// can be displayed;
//====================================================
$opts['fdd']['supplier_id'] = array(
'name' => 'Supplier ID',
'select' => 'D', // Supplier ID will be a dropdown list; hmm... what happens with large # of suppliers
'options' => 'VACPDL',
'maxlen' => 11,
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'supplier_id', // the key field in the joined table
'join' => '$main_table.supplier_id = $join_table.supplier_id',
'description' => 'supplier_id', // the desired column in the joined table
)
);
//====================================================
// Virtual Column 1 - Invoice -> Supplier Name
// ------------------------------------------------
// Uses invoices.supplier_id to link to suppliers.supplier_id
// then grabs the corresponding supplier_name
//====================================================
$opts['fdd']['supplier_id|1'] = array(
'name' => 'Supplier Name',
'input' => 'VR', // because field is V(irtual), it don't exist in the main table
'options' => 'PVDFL',
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'supplier_id', // the key field in the joined table
'description' => 'supplier_name', // the desired column in the joined table
// custom join because PME can't guess the joining field because it's virtual.
'join' => '$main_table.supplier_id = $join_table.supplier_id',
)
);
//====================================================
// Virtual Column 2 - Invoice -> Supplier Address
// ------------------------------------------------
// Uses invoices.supplier_id to link to suppliers.supplier_id
// then grabs the corresponding supplier_address
//====================================================
$opts['fdd']['supplier_id|2'] = array(
'name' => 'Address',
'input' => 'VR', // because field is V(irtual), it don't exist in the main table
'options' => 'PVDFL',
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'supplier_id', // the key field in the joined table
'description' => 'supplier_address', // the desired column in the joined table
// custom join because PME can't guess the joining field because it's virtual.
'join' => '$main_table.supplier_id = $join_table.supplier_id',
)
);
//====================================================
// Virtual Column 3 - Invoice -> Supplier Phone
// ------------------------------------------------
// Uses invoices.supplier_id to link to suppliers.supplier_id
// then grabs the corresponding supplier_phone
//====================================================
$opts['fdd']['supplier_id|3'] = array(
'name' => 'Phone',
'input' => 'VR', // because field is V(irtual), it don't exist in the main table
'options' => 'PVDFL',
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'supplier_id', // the key field in the joined table
'description' => 'supplier_phone', // the desired column in the joined table
// custom join because PME can't guess the joining field because it's virtual.
'join' => '$main_table.supplier_id = $join_table.supplier_id',
)
);
|
-- Tom
|
roland
Založený: 24.03.2006
Príspevky: 25
Zaslal: 2007-05-09 16:02
Návrat hore
Odpovedať s citátom
|
Hello Tom,
I tried to follow your example to get a better impression, how your script works. I created a database, created the tables and put some records in.
Before your script I put this:
| Kód: |
<?php
require_once('config.inc.php'); //connection to database
?>
<h1>testmulti</h1>
<?php
$opts['tb'] = 'invoice'; //WITHOUT THIS I GET AN ERROR 'no table defined'
$main_table = 'invoice';
$join_table = 'supplier';
// Name of field which is the unique key
$opts['key'] = 'invoice_id';
// Type of key field (int/real/string/date etc.)
$opts['key_type'] = 'int';
// Sorting field(s)
$opts['sort_field'] = array('invoice_id');
// Number of records to display on the screen
// Value of -1 lists all records in a table
$opts['inc'] = -1;
// Number of lines to display on multiple selection filters
$opts['multiple'] = '2';
|
Then your code - and then...
| Kód: |
// Now important call to phpMyEdit
require_once '../phpMyEdit.class.php';
new phpMyEdit($opts);
?>
|
When I try to execute the script, I get this:
MySQL error 1054
Unknown column 'PMEjoin.supplier_id' in 'field list'
Any idea, where I make the error?
Roland
|
TomWitt
Založený: 30.08.2004
Príspevky: 5
Zaslal: 2007-05-10 00:14
Návrat hore
Odpovedať s citátom
|
| roland Napísal: |
Hello Tom,
I tried to follow your example to get a better impression, how your script works.
<snip>
When I try to execute the script, I get this:
MySQL error 1054
Unknown column 'PMEjoin.supplier_id' in 'field list'
Any idea, where I make the error?
Roland |
Hey Roland. I can't really tell where the error is happening.
I've included my whole script here (invoice.php) and hopefully that will get you going.
| Kód: |
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>invoice</title>
</head>
<body>
<h3>New Invoices</h3>
<?php
// MySQL host name, user name, password, database, and table
$opts['hn'] = 'localhost';
$opts['un'] = 'db_username';
$opts['pw'] = 'db_password';
$opts['db'] = 'pme_examples';
$opts['tb'] = 'invoice';
// Name of field which is the unique key
$opts['key'] = 'invoice_id';
// Type of key field (int/real/string/date etc.)
$opts['key_type'] = 'int';
// Sorting field(s)
$opts['sort_field'] = array('invoice_id');
// Number of records to display on the screen
// Value of -1 lists all records in a table
$opts['inc'] = 15;
// Options you wish to give the users
// A - add, C - change, P - copy, V - view, D - delete,
// F - filter, I - initial sort suppressed
$opts['options'] = 'ACPVDF';
// Number of lines to display on multiple selection filters
$opts['multiple'] = '4';
// Navigation style: B - buttons (default), T - text links, G - graphic links
// Buttons position: U - up, D - down (default)
$opts['navigation'] = 'UDG';
// Display special page elements
$opts['display'] = array(
'form' => true,
'query' => true,
'sort' => true,
'time' => true,
'tabs' => true
);
// Set default prefixes for variables
$opts['js']['prefix'] = 'PME_js_';
$opts['dhtml']['prefix'] = 'PME_dhtml_';
$opts['cgi']['prefix']['operation'] = 'PME_op_';
$opts['cgi']['prefix']['sys'] = 'PME_sys_';
$opts['cgi']['prefix']['data'] = 'PME_data_';
$opts['language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$opts['fdd']['invoice_id'] = array(
'name' => 'Invoice ID',
'select' => 'T',
'options' => 'AVCPDR', // auto increment
'maxlen' => 11,
'default' => '0',
'sort' => true
);
$opts['fdd']['invoice_date'] = array(
'name' => 'Invoice date',
'select' => 'T',
'maxlen' => 10,
'sort' => true
);
//====================================================
// Multiple Virtual Fields/Columns example
// ------------------------------------------------
// This example shows how a non-key field in the
// main table can be used to reference a foreign key
// in a secondary table and display multiple columns
// from the secondary table as virtual fields/columns
//
// The magic is in the '|<number>' appended to the
// field name of the link field within the main table
//
// I have no idea if this is supposed to work this way
// ... but it does
//====================================================
$main_table = 'invoice';
$join_table = 'supplier';
//====================================================
// Non-key Column in main table - Supplier ID
// ------------------------------------------------
// This is the field we will use as the link from the
// main table to the key field in the join table
//
// Included here to show that the link field itself
// can be displayed;
//====================================================
$opts['fdd']['supplier_id'] = array(
'name' => 'Supplier ID',
'select' => 'D', // Make the supplier ID be a dropdown list
'options' => 'VDRL',
'maxlen' => 11,
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'supplier_id', // the key field in the joined table
'description' => 'supplier_id', // the desired column in the joined table
)
);
//====================================================
// Non-key Column in main table - Supplier ID
// ------------------------------------------------
// This is the field we will use as the link from the
// main table to the key field in the join table
//
// Included here to show that the link field itself
// can be displayed;
//====================================================
$opts['fdd']['supplier_id'] = array(
'name' => 'Supplier ID',
'select' => 'D', // Force the supplier ID be a dropdown list; hmm... what happens with large # of suppliers
'options' => 'VACPDL',
'maxlen' => 11,
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'supplier_id', // the key field in the joined table
'join' => '$main_table.supplier_id = $join_table.supplier_id',
'description' => 'supplier_id', // the desired column in the joined table
)
);
//====================================================
// Virtual Column 1 - Invoice -> Supplier Name
// ------------------------------------------------
// Uses invoices.supplier_id to link to suppliers.supplier_id
// then grabs the corresponding supplier_name
//====================================================
$opts['fdd']['supplier_id|1'] = array(
'name' => 'Supplier Name',
'input' => 'VR', // because field is V(irtual), it don't exist in the main table
'options' => 'PVDFL',
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'supplier_id', // the key field in the joined table
'description' => 'supplier_name', // the desired column in the joined table
// custom join because PME can't guess the joining field because it's virtual.
'join' => '$main_table.supplier_id = $join_table.supplier_id',
)
);
//====================================================
// Virtual Column 2 - Invoice -> Supplier Address
// ------------------------------------------------
// Uses invoices.supplier_id to link to suppliers.supplier_id
// then grabs the corresponding supplier_address
//====================================================
$opts['fdd']['supplier_id|2'] = array(
'name' => 'Address',
'input' => 'VR', // because field is V(irtual), it don't exist in the main table
'options' => 'PVDFL',
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'supplier_id', // the key field in the joined table
'description' => 'supplier_address', // the desired column in the joined table
// custom join because PME can't guess the joining field because it's virtual.
'join' => '$main_table.supplier_id = $join_table.supplier_id',
)
);
//====================================================
// Virtual Column 3 - Invoice -> Supplier Phone
// ------------------------------------------------
// Uses invoices.supplier_id to link to suppliers.supplier_id
// then grabs the corresponding supplier_phone
//====================================================
$opts['fdd']['supplier_id|3'] = array(
'name' => 'Phone',
'input' => 'VR', // because field is V(irtual), it don't exist in the main table
'options' => 'PVDFL',
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'supplier_id', // the key field in the joined table
'description' => 'supplier_phone', // the desired column in the joined table
// custom join because PME can't guess the joining field because it's virtual.
'join' => '$main_table.supplier_id = $join_table.supplier_id',
)
);
$opts['fdd']['invoice_amount'] = array(
'name' => 'Invoice amount',
'select' => 'T',
'maxlen' => 12,
'sort' => true
);
$opts['fdd']['payment_terms'] = array(
'name' => 'Payment terms',
'select' => 'T',
'maxlen' => 7,
'values' => array(
"10 days",
"30 days",
"45 days",
"60 days"),
'sort' => true
);
// Now important call to phpMyEdit
require_once 'phpMyEdit.class.php';
new phpMyEdit($opts);
?>
</body>
</html>
|
|
aeiche
Založený: 06.04.2007
Príspevky: 10
Zaslal: 2007-05-10 19:57
Návrat hore
Odpovedať s citátom
|
I can't get it to work either. I haven't spent a significant amount of time on it though.
I don't think you're dealing with an undocumented feature though. Because your Columns are virtual, they aren't making a reference to a database, only in the definition where the joins are set do they associate with anything. Try changing the names to something else:
IE: make $opts['fdd']['supplier_id|1'] into
$opts['fdd']['in_sup_name'] or something similar and see if it still works.
|
TomWitt
Založený: 30.08.2004
Príspevky: 5
Zaslal: 2007-05-14 16:05
Návrat hore
Odpovedať s citátom
|
| aeiche Napísal: |
I can't get it to work either. I haven't spent a significant amount of time on it though.
I don't think you're dealing with an undocumented feature though. Because your Columns are virtual, they aren't making a reference to a database, only in the definition where the joins are set do they associate with anything. Try changing the names to something else:
IE: make $opts['fdd']['supplier_id|1'] into
$opts['fdd']['in_sup_name'] or something similar and see if it still works. |
Yes, you're absolutely right, aieche. There's no magic in the "|<#>" after all. It is because it's a virtual column that this works.
Thanks for pointing this out.
I'll update the examples in my prior posts to avoid confusion.
-- Tom
|
jvalen
Založený: 09.10.2007
Príspevky: 3
Zaslal: 2007-12-06 03:10
Návrat hore
Odpovedať s citátom
|
Hello:
I try but I get this error:
MySQL error 1054
Unknown column 'PMEtable0.supplier_id|1' in 'field list'
phpMyEdit error: invalid SQL query
SELECT `PMEtable0`.`invoice_id` AS `qf0`,`PMEtable0`.`invoice_date` AS `qf1`,`PMEjoin2`.`supplier_id` AS `qf2`,`PMEtable0`.`supplier_id` AS `qf2_idx`,`PMEjoin3`.`supplier_name` AS `qf3`,`PMEtable0`.`supplier_id|1` AS `qf3_idx`,`PMEjoin4`.`supplier_address` AS `qf4`,`PMEtable0`.`supplier_id|2` AS `qf4_idx`,`PMEjoin5`.`supplier_phone` AS `qf5`,`PMEtable0`.`supplier_id|3` AS `qf5_idx`,`PMEtable0`.`invoice_amount` AS `qf6`,`PMEtable0`.`payment_terms` AS `qf7`,`PMEtable0`.`payment_terms` AS `qf7_idx` FROM `invoice` AS `PMEtable0` LEFT OUTER JOIN `supplier` AS `PMEjoin2` ON (`PMEtable0`.supplier_id = `PMEjoin2`.supplier_id) LEFT OUTER JOIN `supplier` AS `PMEjoin3` ON (`PMEtable0`.supplier_id = `PMEjoin3`.supplier_id) LEFT OUTER JOIN `supplier` AS `PMEjoin4` ON (`PMEtable0`.supplier_id = `PMEjoin4`.supplier_id) LEFT OUTER JOIN `supplier` AS `PMEjoin5` ON (`PMEtable0`.supplier_id = `PMEjoin5`.supplier_id) ORDER BY `PMEtable0`.`invoice_id` LIMIT 0, 1525.13 miliseconds
what is the problem?
thanks in advance
Jorge V.S.'20071205
|
michal
Založený: 17.06.2003
Príspevky: 537
Bydlisko: Slovakia
Zaslal: 2007-12-13 09:21
Návrat hore
Odpovedať s citátom
|
imho, all you need to do is:
| Kód: |
$opts['fdd']['address'] = array(
"sql|VLF" => "`PMEjoin3`.address",
"options" => "VLF",
... plus usual stuff
); |
|
kirk
Založený: 08.04.2009
Príspevky: 1
Zaslal: 2009-04-08 00:33
Návrat hore
Odpovedať s citátom
|
Hi,
I had trouble with the sample provided here as well with an error similar to those listed.
It seems that the virtual field option does not work (at least in my environment) unless you include some sort of 'sql' statement.
For example:
| Kód: |
opts['fdd']['anything_you_like'] = array(
'name' => 'Supplier Name',
'input' => 'VR', // because field is V(irtual), it don't exist in the main table
//'options' => 'PVDFL',
'options' => 'VL',
'sql' => '"bogus_something"', // Can't say why it's needed but it works for me
'escape' => 'false',
'sort' => true,
'values' => Array( // API for currently making a join in PME.
'table' => $join_table, // the table to which we're joining
'column' => 'circuit_id', // the key field in the joined table
'description' => 'vendor', // the desired column in the joined table
// custom join because PME can't guess the joining field because it's virtual.
'join' => '$main_table.circuit_id = $join_table.circuit_id',
)
);
|
Adding this little bit of bogus config works like a charm.
|
|
Odoslať novú tému
Odpovedať na tému
|
|