php - Yii2: how to find by other field in a GridView instead of id field?


I use GridView to show a model called Relatives (familiars of a person) that have two fields: Person (attribute id_person) and Relative (attribute name_relative).

The tables of my database are Persons and Relatives.

I have a problem with the Person field. I don't show the ids of persons, instead I show the names of persons because the id is an auto generated primary key.

When I the user wants to search, it is searched by id_person. I need to search typing a name of a person, nobody will write an id.

Here is the code of the RelativesController file:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
            'attribute' => 'id_person',
            'label' => 'Person',
            'value' => function($model) {
                $person = \app\models\Persons::findOne(['id_person' => $model->id_person]);
                return $person->name_person;
]); ?>

This is what I have (searching by id): Seaching by id

This is what I need (searching by name): Searching by name

People are also looking for solutions to the problem: php - Wordpress - Redirect to previous page after login


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.