Add a variable to table name in editor?

Add a variable to table name in editor?

CapamaniaCapamania Posts: 233Questions: 81Answers: 5
edited August 2016 in Editor

Is it possible instead of using a fix defined table, to add a variable for the table to use in editor? So for instance, I'm sending a variable 'userID' to editor like this:

javascript: 

var = userID;

table = $('#mytable').DataTable( {
        dom: "Blfrtip",
        ajax: {
              url: "myscript.php",
              type: "POST",
              data: function (d) { 
                        d.selectUserID = userID;
                        } },
        serverSide: true,
        ....                             
    } ); 

And then in the editor php: 

// instead of the fix table 'staff': 

$editor = Editor::inst( $db, 'staff' )
    ->fields(
    Field::inst( 'staff.firstname' ),
        Field::inst( 'staff.lastname' ),                
        Field::inst( 'location.account_id' ),
        Field::inst( 'location.account_name' ),
    )

    ->leftJoin( 'location', 'location.id', '=', 'staff.id' )


// add the variable 'userID' to the table name: 

$userID = $_POST["selectUserID"];

$test = 'staff_'$userID;

Editor::inst( $db, $test )
    ->fields(
    Field::inst( $test'.firstname' ),
        Field::inst( $test'.lastname' ),                
        Field::inst( 'location.account_id' ),
        Field::inst( 'location.account_name' ),
    )
    ->leftJoin( 'location', 'location.id', '=', $test'.id' )

Or something like this?

This question has an accepted answers - jump to answer

Answers

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

    Yes, use the pkey() method to set the prefix.

    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5
    edited August 2016

    Allan I'm confused :-)

    I thought pkey() is for defining the primary key in a given/specific table. How can I dynamically alter the table to be use with pkey()?

    Thanks for the help

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

    Sorry! I've been doing work on adding compound keys into Editor for 1.6 and I had pkey in my head! The method you want is called idPrefix().

    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5
    edited August 2016

    No worries. Yet, if I understand idPrefix() correctly .... I don't want to change the html table .... I want to change the mysql database table which should be used within a single html table. Is this also possible?

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

    I completely misunderstood your original question (not doing very well in this thread!). Your original suggestion will work. You need to be careful with security though since it would be trivial for a "hacker" to change the target table.

    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5

    Ah ok. Well I don't want to have any security issues and probably should rethink how I want to do this. Many thanks anyway!

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

    I think it is a valid thing to do - indeed I'm building an app that does something similar at the moment. But I would suggest you keep the table information at the server-side only using a session, as well as ensuring that the user has access to the table they are about to read / write.

    Allan

This discussion has been closed.