One table with two primary key

One table with two primary key

meichichanmeichichan Posts: 5Questions: 1Answers: 1

How to pass the id in "one table with two primary key" case?

Editor::inst( $db, 'RacingGroupRoute' , array('RacingID','GroupID')  )

show error : Primary key data doesn't match submitted data

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 2,296Questions: 18Answers: 255

    The use of compound primary keys is documented here:
    https://editor.datatables.net/examples/advanced/compoundKey

  • meichichanmeichichan Posts: 5Questions: 1Answers: 1

    it's show error : Primary key data doesn't match submitted data

    Ajax Load

    {"data":[{"DT_RowId":"16KB","RacingID":"1057","GroupID":"16KB","GroupName":"\u6e9c\u51b0\u4e59\u7d44","RunningType":2,"Loops":2,"TotalDistance":16,"isSameEnd":1},
    {"DT_RowId":"24KB","RacingID":"1057","GroupID":"24KB","GroupName":"\u6e9c\u51b0\u7532\u7d44","RunningType":2,"Loops":3,"TotalDistance":24,"isSameEnd":1}]}
    

    Javascript

    var editor; 
    $(document).ready(function() {
        
        var racing = "<?php echo $theracing->id; ?>";
        $('#gptable').DataTable( {
            searching: true,
            info: false,
            autoWidth: false,
            dom: '<"top"i>rt<"bottom"<"action left "f>lp><"clear">',
            ajax: {
                url: '/datas/timing/group/'+racing,
                type: 'POST'
            },
            columns: [ 
                        { "data": "RacingID" }, 
                        { "data": "GroupID" }, 
                        { "data": "GroupName" },
                        { "data": "RunningType" , render: function ( data, type, row ) {
                            var result;
                            switch(data)
                            {
                                case 1: result="單趟";
                                break;
                                case 2: result="繞圈";
                                break;
                                case 3: result="接力";
                                break;
                            }
            return result;
            }},
                        { "data": "Loops" },
                        { "data": "TotalDistance" },
                        { "data": "isSameEnd", render: function ( data, type, row ) {
            return data?"是":"否";
        }  }, 
                        { "data": null, render: function (data, type, row){
                            return "<a href='#' class='delete'><i class='fa fa-times'></i></a>";
                        } },
            ]
        } );
        
          
        editor = new $.fn.dataTable.Editor( {
            ajax: "/mngs/timing/edit/group",
            table: "#gptable",  
            fields: [    {
                    name: "RacingID"
                },{
                    name: "GroupID"
                }, {
                    name: "GroupName" 
                }, {
                    name: "RunningType",
                    type:  "select",
                    options: [
                        { label: "單趟", value: 1 },
                        { label: "繞圈",    value: 2 },
                        { label: "接力",    value: 3 }
                    ]
                }, {
                    name: "Loops"
                }, {
                    name: "TotalDistance"
                }, {
                    name: "isSameEnd",
                    type:  "radio",
                    options: [
                        { label: "是", value: 1 },
                        { label: "否",  value: 0 }
                    ] 
                }, {
                    name: "delete"
                }
            ]
        } );
     
        // Activate an inline edit on click of a table cell
        $('#gptable').on( 'click', 'tbody td:not(:last-child)', function (e) {
            editor.inline( this );
        } );
    
    });
    

    Server script

    Editor::inst( $db, 'RacingGroupRoute' , array('RacingID','GroupID')  )
                ->debug( true )
                ->fields(
                    Field::inst( 'RacingID' )->validator( 'Validate::notEmpty' ),
                    Field::inst( 'GroupID' )->validator( 'Validate::notEmpty' ),
                    Field::inst( 'GroupName' )->validator( 'Validate::notEmpty' ),
                    Field::inst( 'RunningType' )->validator( 'Validate::notEmpty' ),
                    Field::inst( 'Loops' )->validator( 'Validate::notEmpty' ),
                    Field::inst( 'TotalDistance' )->validator( 'Validate::notEmpty' ),
                    Field::inst( 'isSameEnd' )->setFormatter( 'Format::ifEmpty', 1 )
                ) 
                ->process( $_POST )
                ->json();
    
  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin

    "DT_RowId":"16KB"

    That looks wrong. It should be a combination of RacingID and GroupID.

    What version of Editor are you using?

    Allan

  • meichichanmeichichan Posts: 5Questions: 1Answers: 1

    Thanks for reply.
    How to combine RacingID and GroupID ?
    Like this? It's still error ><

    "DT_RowId":{"GroupID":"16KB","RacingID":"1057"}
    

    Editor-PHP-1.6.5

  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin

    How to combine RacingID and GroupID ?

    You don't need to combine them at all - it should be doing it automatically for you.

    In this example the two columns are combined thus:

    "DT_RowId": "row_1cab5f07e2016-08-12",
    

    The middle part is a hash that the libraries will automatically calculate.

    Can you show me the JSON that is being loaded from the PHP script when the page is first loaded?

    Thanks,
    Allan

  • meichichanmeichichan Posts: 5Questions: 1Answers: 1

    After I remove "DT_RowID" which I set, it's show Uncaught Unable to find row identifier For more information, please refer to https://datatables.net/tn/14...

    JSON Data (remove "DT_RowID"):
    http://helper.irunner.com.tw/datas/timing/group/1057

  • meichichanmeichichan Posts: 5Questions: 1Answers: 1
    Answer ✓

    I know my problem now.
    Just use Editor to generate json data, not define by myself.
    Thank for your help.

  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin

    Yup - I didn't realise that you were modifying the data from the Editor instance. If you just let it do its thing it should work.

    Allan

  • saee2838saee2838 Posts: 14Questions: 2Answers: 0

    thank
    i need this compound key
    if possible check my code
    when edit row every thing is okey
    but when i want add row show this error

    Primary key data doesn't match submitted data

    i attach table i attach table picture if it help


    thanks

    javascript code

    <script type="text/javascript" language="javascript" class="init">
            var editor; // use a global for the submit and return data rendering in the examples
            $(document).ready(function() {
                var editor;
                var x=-6;
                editor = new $.fn.dataTable.Editor( {
                    ajax: {
                        url: "../includes/datatableseditor/examples/php/event_levels.php",
                        type: "POST",
                        dataType: "json"
                    },
                    table: "#example",
                    fields: [ {
                            label: "شناسه رویداد",
                            name: "sport_calendar_match_levels.event_id"
                        },{
                            label: "شماره مرحله",
                            name: "sport_calendar_match_levels.event_level"
                        },{
                            label: "نام مرحله",
                            name: "sport_calendar_match_levels.level_name"
                        },{
                            label: "زمان شروع",
                            name: "sport_calendar_match_levels.starting_time"
                        }, {
                            label: "زمان پایان",
                            name: "sport_calendar_match_levels.ending_time"
                        }, {
                            label: "تعداد گروه",
                            name: "sport_calendar_match_levels.group_count"
                        }, {
                            label: "الگو گروهبندی",
                            name: "sport_calendar_match_levels.grouping_pattern",
                            type: "select",
                            options: [
                                { label: 'حروف الفبا', value: 'alphabet' },
                                { label: 'نتایج مرحله قبل', value: 'previouslevelplace' },
                                { label: 'نتایج سال قبل', value: 'previousevent' }
                            ]
                        },{
                            label: "شناسه رویداد جهت گروه بندی",
                            name: "sport_calendar_match_levels.grouping_event_id"
                        }, {
                            label: "تعداد صعود کننده از هر گروه",
                            name: "sport_calendar_match_levels.climbers_each_group"
                        }, {
                            label: "تعداد صعود کننده بهترین",
                            name: "sport_calendar_match_levels.climbers_best"
                        }, {
                            label: "نوع رقابت",
                            name: "sport_calendar_match_levels.competition_type",
                            type: "select",
                            options: [
                                { label: 'فردی', value: 'individual' },
                                { label: 'دوگانه', value: 'dual' }
                            ]
                        }
                    ]
                    
                } );
    
                
                var t=$('#example').DataTable( {
                    dom: "Bflrtip",
                    "lengthMenu": [[30, 40, -1], [30, 40, "همه"]],
                    ajax: {
                        url: "../includes/datatableseditor/examples/php/event_levels.php",
                        type: "POST",
                        "deferRender": true,
                        dataType: "json"
                    },
                    language: {
                        "url": "//cdn.datatables.net/plug-ins/1.10.16/i18n/Persian.json",
                        buttons: {
                            copyTitle: 'اطلاعات در حافظه موقت کپی شد!',
                            copyKeys: 'Use your keyboard or menu to select the copy command',
                            copySuccess: {
                                1: "یک ردیف در حافظه موقت ذخیره شد",
                                _: "%d ردیف در حافظه موقت ذخیره شد"
                            }
                        }
                    },
                    select: {
                        style: 'multi'
                    },
                    columnDefs: [ {
                        "searchable": false,
                        "orderable": false,
                        "targets": 0
                        }
                    
                    ],
                    columns: [
                        {
                            data: null,
                            defaultContent: '',
                            className: 'center',
                            orderable: false
                            
                        },
                        { data: "sport_calendar_match_levels.event_id",  orderable: false, className: 'center' },
                        { data: "sport_calendar_match_levels.event_level", className: 'editable center' },
                        { data: "sport_calendar_match_levels.level_name", orderable: false, className: 'editable center' },
                        { data: "sport_calendar_match_levels.starting_time", orderable: false,  className: ' center' },
                        { data: "sport_calendar_match_levels.ending_time", orderable: false  },
                        { data: "sport_calendar_match_levels.group_count", orderable: false, className: 'editable center' },
                        { data: "sport_calendar_match_levels.grouping_pattern", orderable: false, className: 'editable center'},
                        { data: "sport_calendar_match_levels.grouping_event_id", orderable: false,  className: 'editable center' },
                        { data: "sport_calendar_match_levels.climbers_each_group", orderable: false,  className: 'editable center' },
                        { data: "sport_calendar_match_levels.climbers_best", orderable: false, className: 'editable center' },
                        { data: "sport_calendar_match_levels.competition_type", orderable: false, className: 'editable center'}
                    ],
                    
                    select: true,
                    buttons: [
                        { extend: "create", editor: editor },
                        { extend: "edit",   editor: editor },
                        { extend: "remove", editor: editor }
                    ]
                })
            });
        
    </script>
    

    server script



    // 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; $db->sql( "SET NAMES 'utf8'" );//very important for utf8 very important $editor=Editor::inst( $db, 'sport_calendar_match_levels', array('event_level', 'event_id')) ->where( 'event_id', 4 ) ->debug( true ) ->field( Field::inst( 'sport_calendar_match_levels.event_id'), Field::inst( 'sport_calendar_match_levels.event_level' ), Field::inst( 'sport_calendar_match_levels.level_name' ), Field::inst( 'sport_calendar_match_levels.starting_time' ), Field::inst( 'sport_calendar_match_levels.ending_time' ), Field::inst( 'sport_calendar_match_levels.group_count' ), Field::inst( 'sport_calendar_match_levels.grouping_pattern' ), Field::inst( 'sport_calendar_match_levels.grouping_event_id' ), Field::inst( 'sport_calendar_match_levels.climbers_each_group' ), Field::inst( 'sport_calendar_match_levels.climbers_best' ), Field::inst( 'sport_calendar_match_levels.competition_type' ) ) ->validator( function ($editor, $action, $data) { if ( $action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT ) { // Detect duplicates foreach ($data['data'] as $key => $values) { //$values['sport_calendar_match_levels']['event_level'] //$values['sport_calendar_match_levels']['event_id'] $pkey = $editor->pkeyToArray( $key ); // Discount the row being edited if ( $pkey['event_id'] != $values['sport_calendar_match_levels']['event_id'] || $pkey['event_level'] != $values['sport_calendar_match_levels']['event_level'] ) { // Are there any rows that conflict? $any = $editor->db()->any( 'sport_calendar_match_levels', function ($q) use ($pkey, $values) { $q->where( 'event_id', $values['sport_calendar_match_levels']['event_id']); $q->where( 'event_level', $values['sport_calendar_match_levels']['event_level'] ); } ); // If there was a matching row, then report an error if ( $any ) { return 'This staff member is already busy that day.'; } } } } } ) ->process($_POST) ->json();
  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin

    I think you'll need to use:

    array('sport_calendar_match_levels.event_level', 'sport_calendar_match_levels.event_id')
    

    for the primary key identifiers in the constructor.

    Thanks,
    Allan

  • saee2838saee2838 Posts: 14Questions: 2Answers: 0

    thanks for answer
    but not anything change
    error : Primary key data doesn't match submitted data
    when i want insert a row

  • saee2838saee2838 Posts: 14Questions: 2Answers: 0

    can i send my cpanel user and password for you
    for check my code?

Sign In or Register to comment.