Join tables - working with multiple SQL tables

Join tables - working with multiple SQL tables

kringelkringel Posts: 16Questions: 9Answers: 0

Hello.

I'm trying to implement this https://editor.datatables.net/examples/simple/join.html

My code

Javascript

(function($){


    $(document).ready(function() {



    var editor = new $.fn.dataTable.Editor( {
        ajax: 'php/table.users.php',
        table: '#users',
        fields: [
            {
                "label": "\u0411\u0440\u0435\u043d\u0434:",
                "name": "brand"
            },
            {
                "label": "WEB:",
                "name": "web",
                "attr":  {
                    "placeholder": "http://example.com"
                }
            },
            {
                "label": "\u0412\u0438\u0434 \u0434\u0435\u044f\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438:",
                "name": "activity",
                "type": "select",
                placeholder: "Select an option"
            }
        ],
    } );




    var table = $('#users').DataTable( {
        ajax: 'php/table.users.php',
        "columnDefs": [ {
            "targets": 2,
            "data": "download_link",
            "render": function ( data, type, row, meta ) {
                if(row['web'] != null) {
                    return type === "display" ?
                        '<a href="' + data + '" target="_blank">' + data + '</a>' : data;
                    return type === "filter" ?
                        data : data;
                } else {
                    return data;
                }
            }
        }],
        columns: [
            {
                "data": "brand"
            },
            {
                "data": "web"
            },
            {
                "data": "options.activity"
            }
        ],
        select: true,
        scrollX: true,
        autoWidth: true,
        stateSave: true,
        dom:        "<'ui stackable grid'"+

            "<'row'"+
            "<'nine wide column'Q>"+
            ">"+
            "<'row'"+
            "<'eight wide column'B>"+
            "<'right aligned eight wide column'f>"+
            ">"+

            "<'row dt-table'"+
            "<'sixteen wide column'tr>"+
            ">"+

            "<'row'"+
            "<'seven wide column'l>"+"<'two wide column'i>"+
            "<'right aligned seven wide column'p>"+
            ">"+

            ">",

        buttons: [
            { extend: "create", editor: editor },
            { extend: "edit",   editor: editor },
            { extend: "remove", editor: editor },
            { extend: 'colvis',
                text: 'Hide columns'},
            {
                extend: 'excel',
                text: 'Save page in Excel',
                exportOptions: {
                    modifier: {
                        page: 'current'
                    }
                }
            }
        ]
    } );
}(jQuery));

Server script

<?php

/*
 * Editor server script for DB table users
 * Created by http://editor.datatables.net/generator
 */

// DataTables PHP library and database connection
include( "lib/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,
    DataTables\Editor\ValidateOptions;

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'users', 'id' )
    ->fields(
        Field::inst( 'users.brand' )
            ->validator( Validate::notEmpty() ),
        Field::inst( 'users.web' )
                    ->setFormatter( Format::ifEmpty(null) ),

        Field::inst( 'users.activity' )
            ->options( Options::inst()
                ->table( 'options' )
                ->value( 'id' )
                ->label( 'selectable' )
            )
            ->validator( Validate::dbValues() ),
                    Field::inst( 'users.subactivity' )
                        ->setFormatter( Format::ifEmpty( null ) )
                        ->validator( Validate::notEmpty() ),
        Field::inst( 'options.selectable' )
    )
    ->leftJoin( 'options', 'options.id', '=', 'users.activity' )
    ->process( $_POST )
    ->json();

My options table

My users table

I got this error on page reload

What am I doing wrong? Thanks in advance

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    You've got this:

    Editor::inst( $db, 'users', 'id' )
        ->fields(
    

    I think it should be:

    Editor::inst( $db, 'users' )
        ->field(
    

    note field (not fields) and the removal of id,

    Colin

  • kringelkringel Posts: 16Questions: 9Answers: 0

    Thanks. That helped

This discussion has been closed.