Edit feature is not editing the join fields

Edit feature is not editing the join fields

mauro218mauro218 Posts: 9Questions: 5Answers: 0
edited March 2015 in Editor

I'm new using Editor and I'm trying to edit 2 tables with a left join:

This is my code which successfully edit fields from the 'Rolodex' table but not from the 'EmailAddress' table:


<?php

// DataTables PHP library
include $_SERVER["DOCUMENT_ROOT"] . '/Editor-PHP-1.4.0/php/DataTables.php';

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Validate;

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'Rolodex', 'idRolodex' )
    ->fields(
        Field::inst( 'Rolodex.FirstName' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'Rolodex.LastName' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'Rolodex.Notes' ),
        Field::inst( 'Rolodex.Birthday' )
            ->validator( 'Validate::dateFormat', array(
                "format"  => Format::DATE_ISO_8601,
                "message" => "Please enter a date in the format yyyy-mm-dd"
            ) )
            ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
            ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 ),
        Field::inst( 'Rolodex.idEmailAddressMain' ),
        Field::inst( 'EmailAddress.EmailAddress' )
    )
    ->leftJoin( 'EmailAddress', 'EmailAddress.idEmailAddress', '=', 'Rolodex.idEmailAddressMain' )
    ->process( $_POST )
    ->json();


<?php
>
```
______________________________________________________________________________________________________
```
    
?>


        var editor; // use a global for the submit and return data rendering in the examples

        $(document).ready(function() {
            editor = new $.fn.dataTable.Editor( {
                ajax: "rolodex_DTS.php",
                table: "#example",
                fields: [ {
                    label: "First name:",
                    name: "Rolodex.FirstName"
                }, {
                    label: "Last name:",
                    name: "Rolodex.LastName"
                }, {
                    label: "Notes:",
                    name: "Rolodex.Notes"
                }, {
                    label: "Birthday:",
                    name: "Rolodex.Birthday",
                    type: "date"
                }, {
                    label: "Email Address:",
                    name: "EmailAddress.EmailAddress"
                }
                ]
            } );

            $('#example').DataTable( {
                dom: "Tfrtip",
                ajax: "rolodex_DTS.php",
                columns: [
                    { data: null, render: function ( data, type, row ) {
                        // Combine the first and last names into a single table field
                        return data.Rolodex.FirstName+' '+data.Rolodex.LastName;
                    } },

                    { data: "Rolodex.Notes" },
                    { data: "Rolodex.Birthday" },
                    { data: "EmailAddress.EmailAddress" }
                ],
                tableTools: {
                    sRowSelect: "os",
                    aButtons: [
                        { sExtends: "editor_create", editor: editor },
                        { sExtends: "editor_edit",   editor: editor },
                        { sExtends: "editor_remove", editor: editor }
                    ]
                }
            } );
        } );

    </script>

I think my problem is that the 'EmailAddress' table uses idEmailAddress instead of id as the field name. How can I override that? I was able to do it for the 'Rolodex' table in Editor Constructor.

Thanks!

Jhonny

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Hi Jhonny,

    Thanks for the code - that actually looks like it should work. Let me get back to you once I've set up a test for that in my local environment.

    Regards,
    Allan

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Hi Jhonny,

    Just been looking into this further and I think the issue is that the Rolodex.idEmailAddressMain field is not being submitted. I think if you simply add:

    {
      name: 'Rolodex.idEmailAddressMain',
      type: 'hidden'
    }
    

    to your Javascript Editor fields list that should resolve it.

    The reason for this is that the join is constructed slightly unusually for how Editor normally works with data. Typically the idRolodex would be found in the joined table, or the e-mail address wouldn't be editable. That isn't to say it is wrong - far from it - just that it isn't the typical case I see with Editor.

    Regards,
    Allan

  • mauro218mauro218 Posts: 9Questions: 5Answers: 0

    That worked!

    Thanks Allan!

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Good to hear - thanks for letting me know.

    Regards,
    Allan

This discussion has been closed.