Jquery DataTables - 使用查找表连接表

Posted

技术标签:

【中文标题】Jquery DataTables - 使用查找表连接表【英文标题】:Jquery DataTables - Joining tables with lookup table 【发布时间】:2020-02-24 07:59:36 【问题描述】:

我有一个查找(或链接)表learning_event_presentation_lookup

+---------------------------------------+-------------------+-----------------+
| learning_event_presentation_lookup_pk | learning_event_fk | presentation_fk |
+---------------------------------------+-------------------+-----------------+

这是为了方便连接表 learning_eventpresentation

我需要在以下代码的连接中使用该表。

目前我收到错误:

DataTables 警告:表 id=learning_event_table - 发生 SQL 错误:SQLSTATE[42000]:语法错误或访问冲突:1066 Not unique table/alias: 'learning_event'

$( '#learning_event_table' ).DataTable( 
                ajax: "program_data/learning_event_data.php",
                dom: "Bfrtip",
                columns: [ 
                    data: "learning_event.learning_event_name"
                , 
                    data: "learning_event.learning_event_outcome"
                , 
                    data: "rdb_group.rdb_group_name"
                , 
                    data: "presentation.presentation_name"
                , 
                    data: "rotation_discipline_block.rotation_discipline_block_name"
                 ],
                select: 
                    style: 'os',
                    selector: 'td:first-child'
                ,
                buttons: [ 
                    extend: "create",
                    editor: editor
                , 
                    extend: "edit",
                    editor: editor
                , 
                    extend: "remove",
                    editor: editor
                 ]
             );

Editor::inst( $db2, 'learning_event', 'learning_event_pk' )   
    ->field(
     Field::inst( 'learning_event.learning_event_name' ),
     Field::inst( 'learning_event.learning_event_outcome' ),
    Field::inst( 'presentation.presentation_name' ),
     Field::inst( 'learning_event.rdb_group_fk' )
            ->options( Options::inst()
                ->table( 'rdb_group' )
                ->value( 'rdb_group_pk' )
                ->label( 'rdb_group_name' )
            )
            ->validator( 'Validate::notEmpty' ),
        Field::inst( 'rdb_group.rdb_group_name' ),
        Field::inst( 'learning_event.rotation_discipline_block_fk' )
            ->options( Options::inst()
                ->table( 'rotation_discipline_block' )
                ->value( 'rotation_discipline_block_pk' )
                ->label( 'rotation_discipline_block_name' )
            )
            ->validator( 'Validate::dbValues' ),
        Field::inst( 'rotation_discipline_block.rotation_discipline_block_name' )

    )
    ->leftJoin( 'rdb_group', 'rdb_group.rdb_group_pk', '=', 'learning_event.rdb_group_fk' )
    ->leftJoin( 'rotation_discipline_block', 'rotation_discipline_block.rotation_discipline_block_pk', '=', 'learning_event.rotation_discipline_block_fk' )
    ->leftJoin( 'presentation', 'presentation.presentation_pk', '=', 'learning_event_presentation_lookup.presentation_fk' )
    ->leftJoin( 'learning_event', 'learning_event.learning_event_pk', '=', 'learning_event_presentation_lookup.learning_event_fk' )
    ->process($_POST)
    ->json();

【问题讨论】:

如果你把它作为一个 sql 你可以弄明白。它说learning_event 以相同的名称连接了两次。 【参考方案1】:

好的,我在以下位置查看了有关使用链接表的文档:

https://editor.datatables.net/examples/advanced/joinLinkTable.html

所以现在我有以下没有产生任何错误并且工作正常。虽然以 dataTables 格式进行连接并不完全直观......

Editor::inst( $db2, 'learning_event', 'learning_event_pk' )   
    ->field(
     Field::inst( 'learning_event.learning_event_name' ),
     Field::inst( 'learning_event.learning_event_outcome' ),
    Field::inst( 'presentation.presentation_name' ),
     Field::inst( 'learning_event.rdb_group_fk' )
            ->options( Options::inst()
                ->table( 'rdb_group' )
                ->value( 'rdb_group_pk' )
                ->label( 'rdb_group_name' )
            )
            ->validator( 'Validate::notEmpty' ),
        Field::inst( 'rdb_group.rdb_group_name' ),
        Field::inst( 'learning_event.rotation_discipline_block_fk' )
            ->options( Options::inst()
                ->table( 'rotation_discipline_block' )
                ->value( 'rotation_discipline_block_pk' )
                ->label( 'rotation_discipline_block_name' )
            )
            ->validator( 'Validate::dbValues' ),
        Field::inst( 'rotation_discipline_block.rotation_discipline_block_name' )

    )
    ->leftJoin( 'rdb_group', 'rdb_group.rdb_group_pk', '=', 'learning_event.rdb_group_fk' )
    ->leftJoin( 'rotation_discipline_block', 'rotation_discipline_block.rotation_discipline_block_pk', '=', 'learning_event.rotation_discipline_block_fk' )
    ->leftJoin( 'learning_event_presentation_lookup', 'learning_event.learning_event_pk', '=', 'learning_event_presentation_lookup.learning_event_fk' )
    ->leftJoin( 'presentation', 'learning_event_presentation_lookup.presentation_fk', '=', 'presentation.presentation_pk' )
    ->process($_POST)
    ->json();

【讨论】:

以上是关于Jquery DataTables - 使用查找表连接表的主要内容,如果未能解决你的问题,请参考以下文章

如何用 jQuery 中的 dataTables 替换标准引导表?

DataTables 警告:表 id=users - Ajax 错误。使用 jquery 将参数传递给它时

单击按钮时,如何触发 jquery datatables fnServerData 通过 AJAX 更新表?

如何使用 Datatables Jquery 插件销毁表并使用新的更新数据重新初始化同一个表

如何使用 jQuery DataTables 发布整个表的数据

jQuery DataTables 初始化延迟