Custom field options database query in php server script

Custom field options database query in php server script

AllcorAllcor Posts: 16Questions: 5Answers: 0
edited September 2017 in Free community support

in a previous question I was pointed to creating a custom query instead of using the intended method. I created a query in pgadmin that spits out the label and value pairs.

But I can't figure out how to call this query now. Would like to use the $db object but how to reference this?

function originOptions ( $db ) {
    $qText =  "SELECT DISTINCT collectie_origin_pathogen.id AS value, CONCAT(collectie_countrycode.name, ' - (', collectie_origin_pathogen.given_name, ')') AS label ";
    $qText .= "FROM collectie_origin_pathogen ";
    $qText .= "INNER JOIN collectie_countrycode on collectie_origin_pathogen.country_id=collectie_countrycode.id ";
    $qText .= "WHERE is_choice=true ";
    return $db->sql($qText)->fetchAll();
}

...
        Field::inst( 'collectie_collection.origin_id' )
            ->options( function ($db) {
                return originOptions($db);
            })
            -> setFormatter( 'Format::ifEmpty', null )
            ->validator( 'Validate::dbValues', array( 'valid' => array('') ) ),
        Field::inst( 'collectie_countrycode.name' )
...

this results in

Warning: Missing argument 1 for {closure}(), called in /home/naktdata/collectie/DataTablesEditor/Editor/Field.php on line 565 and defined in /home/naktdata/static/collectie/php/isolates.php on line 69

Notice: Use of undefined constant db - assumed 'db' in /home/naktdata/static/collectie/php/isolates.php on line 70

Fatal error: Call to a member function sql() on a non-object in /home/naktdata/static/collectie/php/isolates.php on line 36

where line 69 is the ->options and 36 is the return of originOptions

This question has an accepted answers - jump to answer

Answers

This discussion has been closed.