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;
}
],
'name_relative',
],
]); ?>