How to fix Grid->addQuickSearch() + Field_Callback error?

39 views Asked by At

It seems that Grid->addQuickSearch() method does not work will Field_Callback.

I have the following model:

class Country extends \AppAtk4\MyModel {
    public $title_field = 'calc_title';

    public function init() {
        parent::init();
        $this->addField('iso_code', ['type' => 'string', 'required' => true]); // ISO 3166-1 alpha-2
        $this->addField('name', ['type' => 'string', 'required' => true]);

        $this->addExpression('calc_title', ['CONCAT([iso_code], \' - \', [name])', 'type' => 'string', 'read_only' => true, 'ui' => ['visible' => false]]);
    }
}

And Grid with search input inicialized by this code:

    $crud->setModel($model);

    // add quick search
    $searchFields = [];
    foreach ($model->elements as $name => $element) {
        if (!$element instanceof \atk4\data\Field) {
            continue;
        }

        $searchFields[] = $name;
    }
    $crud->addQuickSearch($searchFields);
    $crud->quickSearch->owner->add(new \atk4\ui\FormField\Input(['inputType' => 'Hidden', 'short_name' => 'crud_model', 'content' => get_class($model)]));

Then when I open the Grid, write some search text and press Search, the following Exception is fired:

atk4\dsql\Exception: DSQL got Exception when executing this query
Exception Parameters
    error: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_sql' in 'where clause'"
    query: "select count(*) from `fromatk4_country` where (`id` like '%s%' or `created_at` like '%s%' or `notes` like '%s%' or `iso_code` like '%s%' or `name` like '%s%' or (CONCAT(`iso_code`, ' - ', `name`)) like '%s%' or `field_sql` like '%s%')"

It seems there is a wrong column name in the SQL query (field_sql). This problem came up one I upgraded to the atk4/data 1.3.5 from some version from last year.

Is this a bug or should/can I inicialize the search fields differently?

I also opened a issue in the project repo: https://github.com/atk4/data/issues/329

1

There are 1 answers

0
romaninsh On

My guess is that this extra field is added by something. Perhaps you're using "$grid->addColumn". In the past this used to add a new decorator for existing fields, now it would add a non-existant field.

It does not seem as your code adds it, so perhaps there is something else. Try commenting things out and using foreach without Grid/CRUD to see if that field is still there.