MJoin - "Unknown Field" on Search & Order

MJoin - "Unknown Field" on Search & Order

btreebtree Posts: 99Questions: 14Answers: 11

Hi guys,

did I miss anything? Can't get a simple Mjoin table to work. Display and Editor works fine, but I cannot order or search the mjoin column. It says "Unknown field: employee".

JS

{
 data: "employee",
 render: "[, ].lastname"
}

PHP

->join(
         Mjoin::inst( 'employee' )
                 ->link( 'customer.id', 'customer_employee.customer_id' )
                 ->link( 'employee.id', 'customer_employee.employee_id' )
                 ->order( 'employee.lastname asc' )
                 ->fields(
                        Field::inst( 'id' )
                                 ->options( 'employee', 'id', 'lastname' ),
                        Field::inst( 'lastname' )
                    )
    )

Any help would be great!

Cheers
Hannes

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Unfortunately there is no option to search and order the Mjoin fields at the moment You have to use columns.searchable and columns.orderable to stop them from triggering this error.

    Allan

  • btreebtree Posts: 99Questions: 14Answers: 11

    Ok, no problem!

    My workaround fyi: I deactivated sortable and searchable and made a custom search field. Think this is even better for the user.

    HTML

    <input type="search" class="form-control input-sm" id="employee_search" placeholder="<?php __e('Employee Lastname'); ?>" data-column="9">
    

    JS

    //Custom Search field for employeer
    $('#employee_search').on( 'keyup click', function () {
       var col = $(this).attr('data-column');
       var search = $(this).val();
        customer_Dtable.column(col).search(search, false).draw();
    } );
    

    PHP

    if(isset($_POST['columns']['9']['search']['value'])){
        $search = (!empty(trim($_POST['columns']['9']['search']['value']))) ? '%'.$_POST['columns']['9']['search']['value'].'%' : '%';
    } else {
        $search = '%';
    }
    
    .....
    if($search != '%'){
        $editor ->leftJoin( 'customer_employee', 'customer.id', '=', 'customer_employee.customer_id' )
        ->leftJoin( 'employee', 'employee.id', '=', 'customer_employee.employee_id' )
        ->where( 'employee.lastname', $search, 'like');
    }
    ...
    $editor ->join(
        Mjoin::inst( 'employee' )
            ->link( 'customer.id', 'customer_employee.customer_id' )
            ->link( 'employee.id', 'customer_employee.employee_id' )
            ->where( 'employee.lastname', $search, 'like')
            ->order( 'employee.lastname asc' )
            ->fields(
                Field::inst( 'id' )
                    ->options( 'employee', 'id', 'lastname' ),
                Field::inst( 'lastname' )
            )
    )
    

    Cheers
    Hannes

This discussion has been closed.