# Editor example Join tables - self referencing join

Extending the simple join example, here we show how a self referencing SQL table can be used with Editor. Self referencing tables can be very useful when there is a hierarchy of like objects in the database.

In this particular example each staff member has a manager, who is also a member of staff. Using a self referencing join we can display each staff member and their manager in the table, while Editor allows these fields to be easily editable.

In SQL terms, the `users` table has a column `manager` which references the `id` column in its own table. In an SQL statement we use the `as` keyword to alias the joined table, which is exactly how the Editor PHP libraries also operate (click the "PHP" tab below the table to see the code used in the server-side script).

The data structure returned by the server for each row in this table is:

    {
    	"DT_RowId": "row_1",
    	"users": {
    		"first_name": "Quynn",
    		"last_name": "Contreras",
    		"manager": "1"
    	},
    	"manager": {
    		"first_name": "Quynn",
    		"last_name": "Contreras"
    	}
    }

The three fields we want to edit are in the `users` object, but we also use the `manager` properties for display in the table (through the use of `columns.dataDT` and `columns.renderDT`).

See the join PHP documentation for further information about joins with Editor's PHP libraries.

First name | Last name | Manager
--- | --- | ---
First name | Last name | Manager

- Javascript
- HTML
- CSS
- Ajax
- Server-side script

The Javascript shown below is used to initialise the table shown in this example:

`var editor; // use a global for the submit and return data rendering in the examples $(document).ready(function() { editor = new $.fn.dataTable.Editor( { ajax: "../php/joinSelf.php", table: "#example", fields: [ { label: "First name:", name: "users.first_name" }, { label: "Last name:", name: "users.last_name" }, { label: "Manager:", name: "users.manager", type: "select" } ] } ); $('#example').dataTable( { dom: "Tfrtip", ajax: "../php/joinSelf.php", columns: [ { data: "users.first_name" }, { data: "users.last_name" }, { data: "manager", render: function ( val, type, row ) { return val.first_name ? val.first_name +' '+ val.last_name : ''; }, defaultContent: "" } ], tableTools: { sRowSelect: "os", aButtons: [ { sExtends: "editor_create", editor: editor }, { sExtends: "editor_edit", editor: editor }, { sExtends: "editor_remove", editor: editor } ] }, initComplete: function ( settings, json ) { // Set the allowed values for the select field based on // what is available in the database editor.field( 'users.manager' ).update( json.userList ); } } ); } );`

In addition to the above code, the following Javascript library files are loaded for use in this example:

- ../../../../media/js/jquery.js
- ../../../../media/js/jquery.dataTables.js
- ../../../TableTools/js/dataTables.tableTools.js
- ../../js/dataTables.editor.js

The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:

This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:

``

The following CSS library files are loaded for use in this example to provide the styling of the table:

- ../../../../media/css/jquery.dataTables.css
- ../../../TableTools/css/dataTables.tableTools.css
- ../../css/dataTables.editor.css

This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.

The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.