[SOLVED] Many to Many Join - PHP

[SOLVED] Many to Many Join - PHP

gforstergforster Posts: 40Questions: 19Answers: 1
edited December 2016 in Free community support

http://debug.datatables.net/ovabuf

I have my "normal" Left Joins working quite smoothly. However, I cannot seem to wrap my head around what I need to do to incorporate a many-many relationship in the editor & datatable.

In this example, I have 3 tables in MySQL. a SERVER table (id, hostname, etc.), a SERVICES table (id, name) and a HOSTSVCS table (id, server_id, service_id). HOSTSVCS exists because a SERVER can have mutliple (or none) SERVICES and any given SERVICE can be on any SERVER.

GOAL: In my table with all the SERVER information, I want to display all associated services and in the Editor field, have the checkboxes for all services listed in the SERVICES table and correctly update the correct values in the HOSTSVCS table (similar to the one to many join example)

The remainder of what I have previously tried is commented out -

My table.php:

 1 servers_table.php                                                                                                                                                                                          X 
 <?php
 
 // DataTables PHP library
 include( "php/DataTables.php" );
 
 // Alias Editor classes so they are easy to use
 use
     DataTables\Editor,
     DataTables\Editor\Field,
     DataTables\Editor\Format,
     DataTables\Editor\Mjoin,
     DataTables\Editor\Options,
     DataTables\Editor\Upload,
     DataTables\Editor\Validate;
 
 // Build our Editor instance and process the data coming from _POST
 Editor::inst( $db, 'SERVERS' )
     ->fields(
         Field::inst( 'SERVERS.id' ),
         Field::inst( 'SERVERS.hostname' )
             ->validator( 'Validate::required' ),
         Field::inst( 'SERVERS.dev_level' ),
         Field::inst( 'SERVERS.os' ),
         Field::inst( 'SERVERS.location_id' )
             ->options( Options::inst()
                 ->table( 'LOCATION' )
                 ->value( 'id' )
                 ->label( 'name' )
             )
             ->validator( 'Validate::dbValues' ),
         Field::inst( 'LOCATION.name' ),
         Field::inst( 'SERVERS.volume_id' )
             ->options( Options::inst()
                 ->table( 'VOLUMES' )
                 ->value( 'id' )
                 ->label( 'name' )
             )
             ->validator( 'Validate::dbValues' )
             ->validator( 'Validate::unique' ),
         Field::inst( 'VOLUMES.name' ),
         Field::inst( 'SERVERS.description' )
     )       
     ->leftJoin( 'LOCATION', 'SERVERS.location_id', '=', 'LOCATION.id' )
     ->leftJoin( 'VOLUMES', 'SERVERS.volume_id', '=', 'VOLUMES.id' )
 //    ->leftJoin( 'HOSTSVCS', 'SERVERS.id', '=', 'HOSTSVCS.server_id' )
 //    ->leftJoin( 'HOSTSVCS', 'SERVICES.id', '=', 'HOSTSVCS.service_id' )
     ->process( $_POST )
     ->json();

The javascript:

 <script type="text/javascript" language="javascript" class="init">
         var editor; 
 
         $(document).ready(function() {
             editor = new $.fn.dataTable.Editor( {
                 ajax: "servers_table.php",
                 table: "#example",
                 fields: [ {
                         label: "Hostname",
                         name: "SERVERS.hostname"
                     }, {
                         label: "Development Level",
                         name: "SERVERS.dev_level",
                         type: "select",
                         options: [
                             { label: "Development", value: "dev" },
                             { label: "Preproduction", value: "preprod" },
                             { label: "Production", value: "prod" }
                         ],
                         placeholder: "What is the Dev Level",
                     }, {
                         label: "OS:",
                         name: "SERVERS.os",
                         type: "select",
                         placeholder: "Select An Operating System",
                         options: [
                             { label: "SLES 11.3", value: "SLES 11.3" },
                             { label: "SLES 11.4", value: "SLES 11.4" },
                             { label: "SLES 12.1", value: "SLES 12.1" },
                             { label: "Windows", value: "WIN"}
                         ],
                     }, {
                         label: "Location:",
                         name: "SERVERS.location_id",
                         type: "select",
                         placeholder: "Select A Location"
                     }, {
                         label: "Volume:",
                         name: "SERVERS.volume_id",
                         type: "select",
                         placeholder: "Select the Volume Name"
                     }, {
                         label: "Services:",
                         name: "HOSTSVCS.service_id",
                         type: "checkbox",
                         placeholder: "Select the Services"
                     }, {
                         label: "description",
                         name: "SERVERS.description"
                     }
                 ]
             } );
             $('#example').DataTable( {
                 dom: "Bfrtip",
                     ajax: {
                         url: "servers_table.php",
                         type: 'POST'
                     },
 
                 columns: [
                     { data: "SERVERS.hostname" },
                     { data: "SERVERS.dev_level" },
                     { data: "SERVERS.os"},
                     { data: "LOCATION.name"},
                     { data: "VOLUMES.name"},
                     //{ data: "HOSTSVCS.service_id"},
                     { data: "SERVERS.description" }
                 ],
                 select: true,
                 buttons: [
                     { extend: "create", editor: editor },
                     { extend: "edit",   editor: editor },
                     { extend: "remove", editor: editor },
                     {
                         extend: 'collection',
                         text: 'Export',
                         buttons: [
                                 'copy',
                                 'excel',
                                 'csv',
                                 'pdf',
                                 'print'
                         ]
                     }
                 ]
             } );
     } );
 </script>

This question has an accepted answers - jump to answer

Answers

  • gforstergforster Posts: 40Questions: 19Answers: 1
    Answer ✓

    Apparently, I need to get stumped, ask the question and then the answer will reveal itself to me.

    This ended up being solved in pretty much the same way the one to many join example is shown. What I didn't realize was the way to use Mjoin properly that the link section of it is where the Join is done, so the solve on the php side was this where HOSTSVCS is the third table of the many to many join linking SERVERS and SERVICES:

         ->join(             
             Mjoin::inst( 'SERVICES' )
                 ->link( 'SERVERS.id', 'HOSTSVCS.server_id' )
                 ->link( 'SERVICES.id', 'HOSTSVCS.service_id' )
                 ->fields(
                     Field::inst( 'id' )
                         ->options( Options::inst()
                             ->table( 'SERVICES' )
                             ->value( 'id' )
                             ->label( 'name' )
                         ),
                     Field::inst( 'name' )
                 )
         )
    
This discussion has been closed.