Get a field value from using 3 different tables in YII2

Yep. That’s right.

Alright boss. Its fine now but i noticed the filter box is not showing. So to add the filter i added “aggregators_name” as one of my attribute in my transaction model, i also added it in my as safe under rules inside my transaction model.

Then inside my TransactionSearch i also added “aggregators_name” to my safe and i added this at the bottom where i have my ->andFilterWhere([‘like’, ‘aggregators_name’, $this->aggregators_name]) .

But i get the error: Getting unknown property: app\models\TransactionsSearch::aggregators_name

Have you defined aggregators_name property by declaring a public member variable of the model?

public $aggregators_name;

(Usually you don’t need to add this in your Transaction model. I would do it in TransactionSearch model.)

But, …

This won’t work, because your transactions table doesn’t have a column named aggregators_name.

Check this wiki article:
Displaying, Sorting and Filtering Model Relations on a GridView

1 Like

Thanks for your help, it finally worked.

1 Like

I have one more question though.

I have a transactions table

below are the fileds in the transactions table
(id,agent_id)

I have an agents table

below are the fileds in the agents table
(id,name, state_id)

I have a states table

below are the fileds in the states table
(id,name, state_id)

What i am trying to do is display the state name for each transation in the transaction Gridview index.php

Below are my model names and their relationships:

Transactions, Agents, States

Below are the relatinships:

Transactions relationship:----

public function getAgent()
{
return $this->hasOne(Agents::className(), [‘ID’ => ‘agent_id’]);
}

Agents Relationships:----

public function getState()
{
return $this->hasOne(States::className(), [‘ID’ => ‘state_id’]);
}

States Relationships:----

public function getAgents()
{
return $this->hasOne(Agents::className(), [‘state_id’ => ‘ID’]);
}

I think you can access the name of the state of an agent like this:

[
    'label' => "Agent's State",
    'value' => 'agent.state.name',
],
// or
[
    'label' => "Agent's State",
    'value' => function ($model) { return $model->agent->state->name; },
],