Editor, primary keys, and updates
Editor, primary keys, and updates
I have a single table that is being added to or edited, but there is a column that represents a part number only by its primary key (unique identifier.) I can put a select drop-down on the key value and populate an array using NAME and VALUE using the actual part number as NAME (so it is relevant to the user,) or I can perform a join and bring the part number in from the part_master table ...
But I can't figure out how to allow the edit that will update the table with the key value, while allowing the user to work with the part number value that is relevant to them.
This is a newbie question, so I'll need some kind of example to help me understand how this works.
But I can't figure out how to allow the edit that will update the table with the key value, while allowing the user to work with the part number value that is relevant to them.
This is a newbie question, so I'll need some kind of example to help me understand how this works.
This discussion has been closed.
Replies
I was able to reconstruct this example copying and pasting code and using the example database I built somewhere in the process of setting up DataTables. One note: I'm pretty sure this example is already built-in to the download package, but I couldn't find it by filenames. I was looking for something with "join" in the title, or something corresponding to the example on the editor.datatables site. Maybe the examples could point me to the specific file in my download package? That would help...
You might find this tutorial useful, as well as the example: http://editor.datatables.net/tutorials/php_join .
The file structure used on the Editor web-site is the same as the download package - so in this case the example is in `examples/join.html` . That example was added in Editor 1.2, so if you have an older version the example wouldn't be there, but with your license you can always download the latest version from http://editor.datatables.net/download :-)
Regards,
Allan
I have a production records table like such:
production_id___count____part_id
970____________500________1
971____________700 _______2
972____________500________1
973____________300________3
And then a part master table that holds the customer part number for the unique identifier we use in part_id:
part_id________part_number
1______________204B
2______________200A
3______________210C
I can do the join with no problem, but the idea is that I need to display the part_number in the table and in the form (because the part_id is irrelevant to the user.) But I need to allow them to change the part number for a given production_id. So the production records table will be updated with a new part_id, and the part master table will be untouched. It's only used to represent the part as the user understands it.
Can you give me an idea of the proper way to approach this?
[code]
$editor = Editor::inst( $db, 'production' )
->field(
Field::inst( 'count' ),
Field::inst( 'part_id' )
)
->join(
Join::inst( 'part', 'object' )
->join( 'part_id', 'part_id' )
->set( false )
->field(
Field::inst( 'part_number' )
)
);
[/code]
Then in the DataTables initialisation you can use `part.part_number` in mData to get the part number for each row.
Not that I've added `set( false )` to the `Join` instance - this tells the classes only to read from the join, not to write to it. So you can freely change `part_id` in the `production` table and the reference will be updated as needs be.
Regards,
Allan
btw, this line "this tells the classes only to read from the join, not to write to it..." taught me a valuable lesson.
Thanks again!
Allan
I'm looking at these sections of code...
From join.php:
[code]
if ( !isset($_POST['action']) ) {
// Get department details
$out['dept'] = $db
->select( 'dept', 'id as value, name as label' )
->fetchAll();
$out['access'] = $db
->select( 'access', 'id as value, name as label' )
->fetchAll();
}
[/code]
From the .js:
[code]
{
"label": "Department:",
// The 'id' value from the property is used to set the value
// of the select list.
"name": "dept.id", // see bottom of page. the table is user_dept and alexandria is dept_id 4 (marketing)
"type": "select"
},
..............................
"fnInitComplete": function ( settings, json ) {
// Set the allowed values for the select and radio fields based on
// what is available in the database
editor.field('dept.id').update( json.dept );
editor.field('access[].id').update( json.access );
}
[/code]
I've been experimenting with it, but can't see how to use it to *populate* the drop-down while only *updating* the parent table.
[code]
var getPartNumbers= new Array({"label" : "a", "value" : "a"});
function partNumSelect(){
getPartNumbers.splice(0,1);
$.ajax({
url: 'tables/Production/AddEditRecord/getPartNumbers.php',
async: false,
dataType: 'json',
success: function (json) {
for(var a=0;a
Allan