iterative link with json field

iterative link with json field

e.jourdee.jourde Posts: 26Questions: 9Answers: 0

Hello, I have a problem. Here is the concept.
a structure file allows in ajax to define the editor fields and the datatable columns and to write the php data access file. the data file is composed of an id and a json where the data is stored.
In this json, there is not necessarily a label, the file can be linked to itself, for example:
id = 1
json = {"prop1": 2, "prop2": 124.56, "prop3": "Label of prop1"}

id = 2
json = {"prop2": 124.56, "prop3": "Label of prop2"}

here i want to display for id = 1 prop1, prop3 of id = 2.

I failed to create a join because of the json.

i have a php function, which brings back the correct wording except i dont know how to call it. i tried a bile :: inst ('libprop1 ") with a getFormatter, but the field not existing in the file, i have an error. I don't want to store this label, just display it. How to display data that is not in the datasource?

sorry for my poor English

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,213Questions: 1Answers: 10,415 Site admin

    A get formatter is the correct thing to do here (I think - I don't really get it to be honest!). Can you show me your get formatter and the error message? Also make sure you add ->set(false) to Fields which you don't want to be editable.

    Allan

  • e.jourdee.jourde Posts: 26Questions: 9Answers: 0

    Hi Allan.

    <?php
    include( "../editor/lib/DataTables.php" );
    use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;
    include '../entete.php';
     
    Editor::inst( $db, 'datahead', 'id_datahead' )
    ->fields(Field::inst( 'datahead.id_datahead')->set(false)
    ,Field::inst( 'datahead.id_datahead_origin')->set(false)
    ,Field::inst( 'datahead.id_context_Model')
    ->setValue($_GET['id_context_model'])
     ,Field::inst("libprop_2602")->set(false)
        ->getFormatter(function($val,$data){
            $tab=json_decode($data->json);
            return c_datahead::lib($tab->prop_2602);
            })
    ,Field::inst( 'json' )
            ->getFormatter(function($val,$data){
               
               
                return json_decode($val);
            })
            ->setFormatter(function($val,$data){
                return json_encode($data["json"]);
            })
                ->validator( function ( $val, $data, $field, $host ) {
                    $json = json_decode(json_encode($val), true);
                    if (json_last_error() != JSON_ERROR_NONE) {
                        return 'JSON validation error';
                    }
                                
                    return true;
                } )
         )
    
    ->where("id_context_model",1,"=")
    
    ->debug( true )
    ->process( $_POST )
    ->json();
    

    and xhr error

    {"fieldErrors":[],"error":"An SQL error occurred: SQLSTATE[42703]: Undefined column: 7 ERROR: column \"libprop_2602\" does not exist\nLINE 1: ....id_context_Model as \"datahead.id_context_Model\", libprop_26...\n ^","data":[],"ipOpts":[],"cancelled":[],"debug":[{"query":"SELECT id_datahead as \"id_datahead\", datahead.id_datahead as \"datahead.id_datahead\", datahead.id_datahead_origin as \"datahead.id_datahead_origin\", datahead.id_context_Model as \"datahead.id_context_Model\", libprop_2602 as \"libprop_2602\", json as \"json\" FROM datahead WHERE id_context_model = :where_0 ","bindings":[{"name":":where_0","value":1,"type":null}]}]}
    

    This is the libprop_2602 field. I am returning the displayed value from the datahead.json field for the prop_2602 property.

    In any case, thank you for responding so quickly.

  • allanallan Posts: 63,213Questions: 1Answers: 10,415 Site admin
    Answer ✓

    Ah I see - you want to call the field libprop_2602, but you don't want to get that from the database? Use getValue() for that:

    Field::inst("libprop_2602")
      ->set(false)
      ->getValue(function () {
        ...
      })
    

    Allan

  • e.jourdee.jourde Posts: 26Questions: 9Answers: 0

    Thank you very much Allan. The complete solution is the combination of getvalue and getformatter

    ,Field::inst("libprop_2602")->set(false)
        ->getValue(function(){return "Not selected";})
        ->getFormatter(function($val,$data){
            $tab=json_decode($data["json"]);
            return c_datahead::lib($tab->prop_2602);
            })
    
This discussion has been closed.