May be a bug in the server script !

May be a bug in the server script !

Yves HEBERTYves HEBERT Posts: 10Questions: 4Answers: 0

I want to limit the elements of a list of 'vDevisPrestation' table with the WHERE instruction with "vDevis_id=70".
Here is the PHP script and the request sent to the database obtained with the option -> debug ('true')

            $out = Editor::inst( $db, 'vDevisLigne' )
                ->debug( true )
                ->fields(
                    Field::inst( 'vDevisLigne.id' )->set(false),
                    Field::inst( 'vDevisLigne.vDevisPrestation_id' )
                            ->options( Options::inst()
                                ->table('vDevisPrestation')
                                ->value('id')
                                ->label('prestation')
                                ->where('vDevis_id',70,'=')
                                 )
                            ->validator( Validate::dbValues() ),
                    Field::inst( 'vDevisLigne.Libelle' ), 
                    ...

in the returned json, the query is :
{"query":"SELECT DISTINCT [id] as 'id', [prestation] as 'prestation' FROM [vDevisPrestation] WHERE [vDevis_id] IS NULL ","bindings":[]}.
it sould be :
{"query":"SELECT DISTINCT [id] as 'id', [prestation] as 'prestation' FROM [vDevisPrestation] WHERE [vDevis_id] = 70 ","bindings":[]}.

My table definition is :

vDevisPrestation
* id
* prestation
* vDevis_id

What can i do ?

Another question : Is it possible to combinate (concatenate) two feilds in the label ('prestation' and 'vDevis_id' for exemple) ?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 50,303Questions: 1Answers: 7,478 Site admin
    Answer ✓

    Is it possible to combinate (concatenate) two feilds in the label ('prestation' and 'vDevis_id' for exemple) ?

    Yes. You can pass an array to the label() method. It will concatenate the values using a space character by default. If that doesn't suit, then there is also a render() method for the Options class that you can use to modify the label.

    ->where('vDevis_id',70,'=')

    Try:

    ->where( function ($q) {
      $q->where('vDevis_id',70,'=');
    })
    

    As the documentation notes the Options->where() method takes a function, not a list of parameters.

    Regards,
    Allan

  • Yves HEBERTYves HEBERT Posts: 10Questions: 4Answers: 0

    Thank you for your reply
    With your response and documentation, I understand the use of a closure function on options.

    But if I replace 70 with a variable $vDevis_id. I have the following return: "error": "Undefined variable: vDevis_id"
    $vDevis_id is well defined. The value is successfully used in the wher contition for 'vDevisLigne' table.
    This is certainly a variable scope problem, but I do not see how to solve it.

      $vDevis = 70;
      $out = Editor::inst( $db, 'vDevisLigne' )
        ->debug( true )
        ->fields(
           Field::inst( 'vDevisLigne.id' )->set(false),
           Field::inst( 'vDevisLigne.vDevis_id' ),
           Field::inst( 'vDevisLigne.vDevisPrestation_id' )
              ->options( Options::inst()
              ->table('vDevisPrestation')
              ->value('id')
              ->label(['id','prestation'])
              ->where(
                    function ($q) {$q->where('vDevis_id',$vDevis_id,'=');})
                   )
              ->validator( Validate::dbValues() ),
                   ...
             )
       ->leftJoin('vDevisPrestation','vDevisLigne.vDevisPrestation_id','=','vDevisPrestation.id' )
       ->where('vDevisLigne.vDevis_id',$vDevis_id)
       ->process( $_POST )
       ->json();
     
    
    
  • allanallan Posts: 50,303Questions: 1Answers: 7,478 Site admin
    Answer ✓

    Correct - with PHP closure functions you have you use a use statement - see the PHP documentation.

    In this case:

    function ($q) use ($vDevis_id) {
     ...
    }
    

    That's just how PHP works.

    Allan

  • Yves HEBERTYves HEBERT Posts: 10Questions: 4Answers: 0

    Thank you very much!

Sign In or Register to comment.