Undefined index in $values array in postCreate event

Undefined index in $values array in postCreate event

TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

I'm getting a system error in a postCreate event where the $values array should contain information from two different tables. The error is caused by invalid JSON in the response. The JSON is invalid because of an Undefined index: tickets error in the $values array returned to the postCreate event. I have a very similar structure in a different application which works correctly, and I can't see what's wrong here.

The fields definition in my editor is:

            fields: [
                {
                    label: "ticket Id",
                    name: "notes.ticket_id",
                    type: "readonly"
                },
                {
                    label: "commenter:",
                    name: "notes.email",
                    def: function() {
                        var user = $('#theUser').attr('data-user');
                        return user;
                        }
                },
                {
                    label: "new note:",
                    name: "notes.content",
                    type: "textarea"
                },
                {
            name: 'tickets.email',
            type: 'hidden',
            def: function () {
                return tickets.email;
            }
                },
                {
                    label: "resolved?:",
                    name: "notes.resolved",
                    type: "select",
                    placeholder: "yes/no",
                    options: [
                        "no",
                        "yes"
                    ],
                    def: "no"
                }
            ]

The relevant PHP code in my Ajax routine is:

    Editor::inst( $db, 'notes', 'id' )
        ->fields(
            Field::inst( 'notes.id' )
                ->set(false),
            Field::inst( 'notes.created' )
                ->setValue(date("Y-m-d H:i:s"))
                ->validator( 'Validate::dateFormat', array( 'format'=>'Y-m-d H:i:s' ) )
                ->getFormatter( 'Format::datetime', array( 'from'=>'Y-m-d H:i:s', 'to'  =>'Y-m-d H:i:s' ) )
                ->setFormatter( 'Format::datetime', array( 'to'  =>'Y-m-d H:i:s', 'from'=>'Y-m-d H:i:s' ) ),
            Field::inst( 'notes.content' ),
            Field::inst( 'notes.resolved' ),
            Field::inst( 'notes.email' ),
            Field::inst( 'notes.ticket_id' )
                ->options( 'tickets', 'id', 'id' )
                ->validator( 'Validate::dbValues' ),
            Field::inst( 'tickets.email' ),
            Field::inst( 'tickets.id' )
        )
    ->on('postCreate', function($editor,$id,$values,$row) { // Notify techs and originator of new note
        notify($values['notes']['ticket_id'], $values['tickets']['email'], $values['notes']['content']);
    } )
        ->leftJoin('tickets','tickets.id','=','notes.ticket_id')        // Fetch notes for the selected ticket
        ->where('ticket_id',$_POST['ticket'])
        ->process( $_POST )
        ->json();

The error message is: A system error has occurred (More information).

The JSON response is:

<br />
<b>Notice</b>:  Undefined index: tickets in <b>/var/www/vhosts/bajzek.com/asccintranet.bajzek.com/ITTracker/php/table.notes.php</b> on line <b>87</b><br />
{"data":[{"DT_RowId":"row_2854","notes":{"id":"2854","created":"2019-05-03 08:32:29","content":"Testing notification.","resolved":"no","email":"tom","ticket_id":"4128"},"tickets":{"email":"cory","id":"4128"}}]}

It appears that the correct information is returned in the Ajax response, insofar as the tickets data is provided, but I don't see the reason for the 'tickets' index to be invalid. What is the actual problem here?

Thanks,
Tom

Answers

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

    Are you inline editing perhaps? That (by default) will submit only the field which has changed, so if you change a notes field value, the client-side wouldn't be submitting the tickets object.

    I'd suggest a simple solution - use $row['tickets']['email'] rather than $values.... $row is what was read from the db, while $values is what was submitting from the client-side.

    Allan

  • TomBajzekTomBajzek Posts: 164Questions: 37Answers: 1

    Allan,

    It's not inline editing. I really don't want to change anything in the tickets table here. I just need to use the email field from the tickets table as the email address for the notification I want to send. If I leave out the tickets.id from the PHP, I get "Requested unknown parameter 'tickets.id' for row 0, column 1" when I try to load the notes editor. Thus, I have included it.

    I tried your solution, and it did work. This leads me to other questions:

    1. Does this mean that tickets.email was submitted and saved, and thereby returned in the $row array? (Saving this is unnecessary, but it won't hurt because I'm not allowing tickets.email to be changed.)
    2. Is there a proper way to pass a parameter to the Ajax routine so that it could just be used without being stored into some database table? (This is what I'm actually trying to achieve here. The tickets.email data is not used in the notes table, but it is where the notification is to be sent.)

    Thanks,
    Tom

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

    Does this mean that tickets.email was submitted and saved, and thereby returned in the $row array?

    Basically yes. When you update a row using the Editor libraries it will then query the database to get the latest data for that row (including the updated field - it doesn't just echo the field back to the client). This is for two reasons:

    1. Security - you might use formatting to help protect against XSS for example.
    2. You might have database updated fields in the table (e.g. last modified time).

    Is there a proper way to pass a parameter to the Ajax routine so that it could just be used without being stored into some database table?

    Yes, use preSubmit to modify the data object being sent to the server. You can then read the data from $_POST as normal.

    Allan

This discussion has been closed.