php - Yii2: how to find by other field in a GridView instead of id field?
Get the solution ↓↓↓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',
],
]); ?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xml parsing error: no root element found
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.
Similar questions
Find the answer in similar questions on our website.
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.