How to join correctly?
How to join correctly?
I have a database with two tables:
StaticInformation with fields: id, name, typeID
StaticInformationType with fields: id, name
I want to display a table with the information coming from the StaticInformationTable but on the typeID column I want to display the name of the type instead of the typeID In other words, the name that correspondents to the id in the StaticInformationType table. typeID is a reference to id in the StaticInformationType table.
I have now the following code:
table.StaticInformation.js:
[code]
var editor;
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "table.StaticInformation.php",
"domTable": "#StaticInformation",
"fields": [
{
"label": "Type:",
"name": "StaticInformationType.id",
"type": "select"
},
{
"label": "Name:",
"name": "name"
}
]
} );
$('#StaticInformation').dataTable( {
"sDom": "Tfrtip",
"sAjaxSource": "table.StaticInformation.php",
"aoColumns": [
{
"mData": "StaticInformationType.name",
"sDefaultContent": ""
},
{ "mData": "name" }
],
"oTableTools": {
"sRowSelect": "multi",
"aButtons": [
{ "sExtends": "editor_create", "editor": editor },
{ "sExtends": "editor_edit", "editor": editor },
{ "sExtends": "editor_remove", "editor": editor }
]
},
"fnInitComplete": function ( settings, json ) {
editor.field('StaticInformationType.id').update( json.StaticInformationType );
}
} );
} );
[/code]
table.StaticInformation.php:
[code]
<?php
include( "extras/Editor-1.2.4/examples/php/lib/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
$editor = Editor::inst( $db, 'StaticInformation' )
->field(
Field::inst( 'typeID' ),
Field::inst( 'name' )
)
->join(
Join::inst( 'StaticInformationType', 'object' )
->join( 'typeID', 'id' )
->field(
Field::inst( 'name' )
)
);
$out = $editor
->process($_POST)
->data();
if ( !isset($_POST['action']) ) {
$out['StaticInformationType'] = $db
->select( 'StaticInformationType', 'id as value, name as label' )
->fetchAll();
}
echo json_encode( $out );
?>
[/code]
This basically works but when I change or add something, it alters the StaticInformationType table as well. This should be fixed and only the typeID must be changed in the StaticInformation table but this goes wrong also.
Hopefully someone can help me with is. I already spend several hours get what I have now but I cannot solve my problem....
Thanks!
Olan
StaticInformation with fields: id, name, typeID
StaticInformationType with fields: id, name
I want to display a table with the information coming from the StaticInformationTable but on the typeID column I want to display the name of the type instead of the typeID In other words, the name that correspondents to the id in the StaticInformationType table. typeID is a reference to id in the StaticInformationType table.
I have now the following code:
table.StaticInformation.js:
[code]
var editor;
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "table.StaticInformation.php",
"domTable": "#StaticInformation",
"fields": [
{
"label": "Type:",
"name": "StaticInformationType.id",
"type": "select"
},
{
"label": "Name:",
"name": "name"
}
]
} );
$('#StaticInformation').dataTable( {
"sDom": "Tfrtip",
"sAjaxSource": "table.StaticInformation.php",
"aoColumns": [
{
"mData": "StaticInformationType.name",
"sDefaultContent": ""
},
{ "mData": "name" }
],
"oTableTools": {
"sRowSelect": "multi",
"aButtons": [
{ "sExtends": "editor_create", "editor": editor },
{ "sExtends": "editor_edit", "editor": editor },
{ "sExtends": "editor_remove", "editor": editor }
]
},
"fnInitComplete": function ( settings, json ) {
editor.field('StaticInformationType.id').update( json.StaticInformationType );
}
} );
} );
[/code]
table.StaticInformation.php:
[code]
<?php
include( "extras/Editor-1.2.4/examples/php/lib/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
$editor = Editor::inst( $db, 'StaticInformation' )
->field(
Field::inst( 'typeID' ),
Field::inst( 'name' )
)
->join(
Join::inst( 'StaticInformationType', 'object' )
->join( 'typeID', 'id' )
->field(
Field::inst( 'name' )
)
);
$out = $editor
->process($_POST)
->data();
if ( !isset($_POST['action']) ) {
$out['StaticInformationType'] = $db
->select( 'StaticInformationType', 'id as value, name as label' )
->fetchAll();
}
echo json_encode( $out );
?>
[/code]
This basically works but when I change or add something, it alters the StaticInformationType table as well. This should be fixed and only the typeID must be changed in the StaticInformation table but this goes wrong also.
Hopefully someone can help me with is. I already spend several hours get what I have now but I cannot solve my problem....
Thanks!
Olan
This discussion has been closed.
Replies
What you can do is use the `set()` method of the Join class to tell Editor to not change any information on the joined table - https://editor.datatables.net/docs/current/php/class-DataTables.Editor.Join.html#_set .
So in this case:
[code]
$editor = Editor::inst( $db, 'StaticInformation' )
->field(
Field::inst( 'typeID' ),
Field::inst( 'name' )
)
->join(
Join::inst( 'StaticInformationType', 'object' )
->join( 'typeID', 'id' )
->set( false )
->field(
Field::inst( 'name' )
)
);
[/code]
The other thing is:
> "name": "StaticInformationType.id"
I think you probably want `"name": "typeID"` here, so it is updating the base table.
Regards,
Allan
Thank you! Almost there I think but: when I set "name": "typeID" my select list is empty. When I use "name": "StaticInformationType.id" I have a list but when I make a new entry it doesn't set the id from StaticInformationType into my table.
regards,
Olan
Did you modify the `update()` call to reflect the change in name as well?
Allan
Olan
Allan
Thanks!
Sasmit
An example like the one in this post would be super useful in the Editor examples section, since I think using joins for viewing and editing are two very different, but very useful things that Editor has to offer. Thanks for all your hard work Allan!
Allan