Joining tables with lookup table
Joining tables with lookup table
I have a lookup table learning_event_presentation_lookup
:
+---------------------------------------------------+-----------------------+--------------------+
| learning_event_presentation_lookup_pk | learning_event_fk | presentation_fk |
+---------------------------------------------------+-----------------------+--------------------+
This is to facilitate joining tables learning_event
and presentation
.
I need to use that table in a join in the following code. Currently I'm getting the error:
DataTables warning: table id=learning_event_table - An SQL error occurred: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'learning_event'
$( '#learning_event_table' ).DataTable( {
ajax: "program_data/learning_event_data.php",
dom: "Bfrtip",
columns: [ {
data: "learning_event.learning_event_name"
}, {
data: "learning_event.learning_event_outcome"
}, {
data: "rdb_group.rdb_group_name"
}, {
data: "presentation.presentation_name"
}, {
data: "rotation_discipline_block.rotation_discipline_block_name"
} ],
select: {
style: 'os',
selector: 'td:first-child'
},
buttons: [ {
extend: "create",
editor: editor
}, {
extend: "edit",
editor: editor
}, {
extend: "remove",
editor: editor
} ]
} );
and
Editor::inst( $db2, 'learning_event', 'learning_event_pk' )
->field(
Field::inst( 'learning_event.learning_event_name' ),
Field::inst( 'learning_event.learning_event_outcome' ),
Field::inst( 'presentation.presentation_name' ),
Field::inst( 'learning_event.rdb_group_fk' )
->options( Options::inst()
->table( 'rdb_group' )
->value( 'rdb_group_pk' )
->label( 'rdb_group_name' )
)
->validator( 'Validate::notEmpty' ),
Field::inst( 'rdb_group.rdb_group_name' ),
Field::inst( 'learning_event.rotation_discipline_block_fk' )
->options( Options::inst()
->table( 'rotation_discipline_block' )
->value( 'rotation_discipline_block_pk' )
->label( 'rotation_discipline_block_name' )
)
->validator( 'Validate::dbValues' ),
Field::inst( 'rotation_discipline_block.rotation_discipline_block_name' )
)
->leftJoin( 'rdb_group', 'rdb_group.rdb_group_pk', '=', 'learning_event.rdb_group_fk' )
->leftJoin( 'rotation_discipline_block', 'rotation_discipline_block.rotation_discipline_block_pk', '=', 'learning_event.rotation_discipline_block_fk' )
->leftJoin( 'presentation', 'presentation.presentation_pk', '=', 'learning_event_presentation_lookup.presentation_fk' )
->leftJoin( 'learning_event', 'learning_event.learning_event_pk', '=', 'learning_event_presentation_lookup.learning_event_fk' )
->process($_POST)
->json();
This question has an accepted answers - jump to answer
Answers
UPDATE
OK, I looked at the documenation on using a linked table at:
https://editor.datatables.net/examples/advanced/joinLinkTable.html
So now I have the following which is not producing any errors and working correctly.
Except that in the editor template,
presentation.presentation_name
is not showing in the select.with
and
UPDATE 2
Now using the following, which is good on the editor template side, but still can't get the names to show in the table for two columns from the link table:
and
and
However, in the table, only the ID (pk) of the prersentation and mcondition are showing, not the names. How do I get the names to display?
In the Editor template everything shows OK:
Going back to the original for a moment, the issue was with the use of
which is a self referencing join. That's possible, but you need to use an alias.
You want to reference the joined data in your
columns.data
property. e.g.:note you'll need to add a
Field::inst()
on the server-side to get that field's data.Allan