use primarykey in setFormatter

use primarykey in setFormatter

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

Hello everyone, I have a problem that seems silly to me but I cannot find the solution in the doc.
I made a setFormatter with a query, but I need the primary key of the current record and I don't know how to access it, it must be very stupid, but I can't find it.

Editor::inst( $db, 'familytree', 'id_familytree' )             <======primarykey=id_familytree
->fields(
    Field::inst( 'id_familytree' )->set(false),                   <======primarykey=id_familytree
    Field::inst( 'id_translate' )
    ->getFormatter(function($val,$data) use ($db){
        $statement =
         ('SELECT translate_language.translation as label
                           FROM translate_language
                          WHERE  translate_language.id_translate=:val
                           and translate_language.id_language=:id_language                         
                       ');
        $result =
        $db ->raw()
        ->bind(':val',$val )
        ->bind(':id_language',$_GET["langue"])
        ->exec($statement);
         
        $res=$result->fetchAll(PDO::FETCH_ASSOC);
        return $res[0]["label"];
    })
    ->setFormatter( function ( $val, $data, $opts ) use ($db){
        $statement =
         ('update translate_language,familytree set translation=:val
                          WHERE  translate_language.id_translate=familytree.id_translate
                           and translate_language.id_language=:id_language                     
                            and familytree.id_familytree=:id
                       ');
        $result =
        $db ->raw()
        ->bind(':val',$val )
        ->bind(':id',Field("id_familytree"))    <=======here, how acced to value of primary key
        ->bind(':id_language',$_GET["langue"])
        ->exec($statement);
        return $val;
    } )
    
    
   ) 
   ->debug(true)
    ->process( $_POST )
    ->json();

This question has an accepted answers - jump to answer

Answers

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

    I found a rather unattractive solution for my taste, but it works! If you have another method I am taker.

     ->setFormatter( function ( $val, $data, $k ) use ($db){
            $k=0;
            if(isset($_POST["data"])){
                $d=$_POST["data"];
                $tk=explode("_",key($d));
                $k=$tk[1];
            
            }
    //$k is the primary key's value
    ...
    
  • allanallan Posts: 61,723Questions: 1Answers: 10,108 Site admin
    Answer ✓

    I'd never really expected the set formatter to be used in such as way, so there isn't actually a method to get the primary key value. That looks like a bit of an oversight and I'll have a think about that, but the set formatter had really been intended for simple transforms - e.g. one date format into another.

    The set formatter you have there isn't actually doing any formatting of the value, so I'd recommend not using a set formatter, but rather one of the server-side events (preEdit or postEdit for example - likewise for create) which are designed for doing the kind of thing you are.

    Allan

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

    Thanks Allan, I will watch this.

This discussion has been closed.