Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82449

Join tables - one-to-many join

$
0
0

I have 3 tables, im trying to join one to many but cant get me head around it. What am i doing wrong?

TABLES: members - id, firstname, lastname etc

guns - id, name

member_guns - member, gun

JOIN ON: member_guns.member = members.id

member_guns.gun = guns.id

<script type="text/javascript">
var editor; // use a global for the submit and return data rendering in the examples
 
$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: "database/connections/members.php",
        table: "#example",
        fields: [ {
                label: "First name:",
                name: "firstname"
            }, {
                label: "Last name:",
                name: "lastname"
            }, {
                label: "Address:",
                name: "address"
            }, {
                label: "FAC Number:",
                name: "fac"
            }, {
                label: "Membership:",
                name: "membership"
            }, {
                label: "Guns:",
                name: "guns[].id",
                type: "checkbox"
            }
        ]
    } );
 
    $('#example').DataTable( {
        dom: "Tfrtip",
        ajax: "database/connections/members.php",
        columns: [
            { data: null, render: function ( data, type, row ) {
                // Combine the first and last names into a single table field
                return data.firstname+' '+data.lastname;
            } },
            { data: "address" },
            { data: "fac" },
            { data: "membership" },
            { data: "guns", render: "[, ].name" }
        ],
        tableTools: {
            sRowSelect: "os",
            aButtons: [
                { sExtends: "editor_create", editor: editor },
                { sExtends: "editor_edit",   editor: editor },
                { sExtends: "editor_remove", editor: editor }
            ]
        }
    } );
} );
</script>



Editor::inst( $db, 'members' )
    ->fields(
        Field::inst( 'firstname' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'lastname' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'address' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'fac' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'membership' )->validator( 'Validate::notEmpty' )
    )
    ->join(
        Join::inst( 'guns', 'array' )
            ->join(
                array( 'id', 'gun' ),
                array( 'id', 'member' ),
                'member_guns'
            )
            ->fields(
                Field::inst( 'id' )->validator( 'Validate::required' ),
                Field::inst( 'name' )
            )
    )
    ->process($_POST)
    ->data();

Viewing all articles
Browse latest Browse all 82449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>