Table alias in leftJoin()

Table alias in leftJoin()

milapmilap Posts: 40Questions: 13Answers: 2
edited January 2016 in Editor

Hello,
Is it possible to put table alias inside leftJoin() function?

I have code like this:

$editor = Editor::inst( $db, 'operation' )
    ->fields(
                Field::inst( 'operation.id_user' ),
                Field::inst( 'user.lastname' ),
                Field::inst( 'user.name' ),
                Field::inst( 'operation.id_ses_user' ),
                Field::inst( 'ses.lastname' ),
                Field::inst( 'ses.name' ),
        )              
    ->leftJoin( 'user', 'user.id', '=', 'operation.id_user' )
        ->leftJoin( 'user as ses', 'ses.id', '=', 'operation.id_ses_user' ) //HERE IS ALIAS: user as ses
    ->process( $_POST );
$editor->json();

When I am running pure sql code like this on database :

select user.lastname, user.name, ses.lastname, ses.name
from operation
left join user
on operation.id_user = user.id
left join user as ses
on operation.id_ses_user = ses.id

it returns proper data.

part of my js code:

columns: [
    { data: null, render: function ( data, type, row ) {
            // Combine the first and last names into a single table field
            return data.user.lastname+' '+data.user.name;
    } }, //TILL HERE CODE WORKS FINE. ADDING CODE BELOW GIVES AN ERROR
    { data: null, render: function ( data, type, row ) {
            // Combine the first and last names into a single table field
            return data.ses.lastname+' '+data.ses.name;
    } }
],

Browser console returns: Uncaught TypeError: Cannot read property 'lastname' of undefined
Any idea why? Can I use alias in such way or do I have to write custom sql query?

This question has an accepted answers - jump to answer

Answers

  • milapmilap Posts: 40Questions: 13Answers: 2
    edited January 2016 Answer ✓

    Ok,
    My mistake.

    After I have confirm by my self that aliasing in leftJoin works:
    https://editor.datatables.net/examples/advanced/joinSelf.html
    I have found stupid error that I have made.

    When I was coping files to create new page I have't changed
    ajax: "ajax/some_file.php" to new one.
    My bad, sorry for bothering....

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

    No worries - good to hear you've got it working now.

    Allan

This discussion has been closed.