PHP code: Comparison with the value of a variable inside WHERE. Is it possible?

PHP code: Comparison with the value of a variable inside WHERE. Is it possible?

ostmalostmal Posts: 102Questions: 33Answers: 0

The PHP code receives the following variables:
$_POST ['user_id'] - user id
$_POST ['flag_global'] - visibility flag: 0-sees only its own records, 1-sees all records
The logic is as follows:
If $_POST ['flag_global'] == 1, the user sees ALL the records.
If $_POST ['flag_global'] == 0, the user sees entries with the field "user_id_create" = $_POST ['user_id'].
I would like to write this logic inside WHERE, but I do not know how.
Of course, you can initially prescribe:

If ( Если $_POST['flag_global'] == 1 ) {
    Editor::inst( $db, 'crm_note' )
    ….
        ->process($_POST)
        ->json();
}
else {
    Editor::inst( $db, 'crm_note' )
    …
        ->where( 'user_id_create', $user_id )
        ->process($_POST)
        ->json();
}

But then you will have to register a lot of fields twice and then make changes twice when changing the code.

Answers

  • ItracItrac Posts: 12Questions: 1Answers: 0

    You can do it like this:

    $editor = Editor::inst($db, 'crm_note')
        ->field(
            Field::inst('field'), //example field
            Field::inst('field1'), //example field
            Field::inst('field2') //example field
        );
    if ($_POST['flag_global'] == 0) {
        $editor
            ->where('user_id_create', $user_id);
    }
    $editor
        ->process($_POST)
        ->json();
    
  • ostmalostmal Posts: 102Questions: 33Answers: 0

    This is obvious at first glance ))) I'm used to the chain. Thank you, Itrac!

Sign In or Register to comment.