Firstname Lastname Fullname search in ATK


Hey there,

I think this is a very commonly known problem and has been discussed in general IT a lot of times. It would surely make a good ATK tutorial.

Ok, its about:
In the Database, I store firstname and lastname of a person. I also have a search field in which persons can be searched.

Lets assume a person is called “John Smith”.
Now I want all 3 searches to return him as result:
“John Smith”

So search either in firstname, lastname or firstname + lastname.

I will start digging into that now, if anyone has done this already please gimme a hint :slight_smile:

Best regards


Typically you’ll want to maintain a “search” field:

$model->addField('search', ['system'=>true]);

// When saving update search field
$model->addHook('beforeSave', function($model) {
    $model['search'] = $model['name'].' '.$model['surname']; 

If you use SQL, add fulltext index and use this:

$model->addCondition($model->expr('[search] like [s]', ['s'=>'John']));




thought of this as its pretty obvious to add a field to the database like this. Thought there might be a “smarter” way, but reconsidering this extra field is extremely useful, e.g. displaying a list of names, sorting by first- and lastname and so on.

Thanks for your help.


For SQL, having a hidden field like that is a best practice. Some databases can search through all fields.