Combining joinarray and table only data code

Combining joinarray and table only data code

nigel pasconigel pasco Posts: 37Questions: 6Answers: 0
edited August 2014 in Free community support

Hi,
I was trying to get the "JoinArray.html" example to work with the adding table only data from the "tableOnlyData.html" example, as I need to combine these two features on my site. My attempt is as below, however, obviously is not working. The major change is the change from the data() call to the json() call, which I assume causes the error, however, can not find any reference as to how to do this correctly.

Anyone able to have a quick look and recommend a solution?

Thank you
nige

<?php

// DataTables PHP library
include( "../../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;


/*
 * Example PHP implementation used for the join.html example
 */
$out = Editor::inst( $db, 'users' )
    ->field(
        Field::inst( 'users.last_name' ),
        Field::inst( 'users.site' ),
        Field::inst( 'sites.name' )
    )
    ->leftJoin( 'sites', 'sites.id', '=', 'users.site' )
    ->join(
        Join::inst( 'access', 'array' )
            ->join(
                array( 'id', 'user_id' ),
                array( 'id', 'access_id' ),
                'user_access'
            )
            ->fields(
                Field::inst( 'id' )->validator( 'Validate::required' ),
                Field::inst( 'name' )
            )
    );

// I ADDED THE FOLLOWING CODE AS PER THE 'TABLE ONLY DATA' EXAMPLE TO TRY COMBINE BOTH FEATURES...
if ( isset($_POST['action']) && $_POST['action'] === 'create' ) {
    // Adding a new record, so add a field that will be written

    $out
        ->fields(
            Field::inst( 'users.fname' )
        );
    $_POST['data']['users']['fname'] = "william";
}
else {
    // Otherwise editing, deleting or getting data. Just read the owner field
    $out
        ->fields(
            Field::inst( 'users.fname' )->set(false)
        );
}

$out
    ->process($_POST)
    ->json();

//END OF 'TABLE ONLY DATA EXAMPLE TYPE CODE'

// When there is no 'action' parameter we are getting data, and in this
// case we want to send extra data back to the client, with the options
// for the 'sites' and 'dept' select lists
if ( !isset($_POST['action']) ) {
    // Get a list of sites for the `select` list
    $out['sites'] = $db
        ->selectDistinct( 'sites', 'id as value, name as label' )
        ->fetchAll();

    // Get department details
    $out['access'] = $db
        ->select( 'access', 'id as value, name as label' )
        ->fetchAll();
}

// Send it back to the client
echo json_encode( $out );

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    Answer ✓

    You mentioned you are getting an error. What is that error?

    I think you probably want to change:

    $out
    ->process($_POST)
    ->json();

    to:

    $out = $editor
        ->process($_POST)
        ->data();
    

    and change $out = Editor::inst( $db, 'users' ) to:

    $editor = Editor::inst( $db, 'users' )
    

    At the moment it is probably outputting invalid JSON. The json() Editor method outputs the JSON - but you want to output it yourself, so use data() to get the data.

    Allan

  • nigel pasconigel pasco Posts: 37Questions: 6Answers: 0

    Thank you Allan, that did work. It seems odd to use $out = $editor, as it seems you are just changing the name of the same variable... but it does work. Well played. nige

  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin

    Well you can all the variable anything you want. However, the key difference in the above code is what the variable contains. $editor is the Editor class instance and $out is the data array. It isn't just $out = $editor but rather the result from the chained process() and data() methods.

    Allan

This discussion has been closed.