Display the state name for each transaction 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’]);
}

So what’s the problem?

You should have State relationship in transaction model. I hope there is direct relationship between transaction and states.
To be sure, share your gridview transaction/index.php, transaction and state model.

What i want to do is Display the state name for each transaction in the transaction Gridview index.php

Thats the issue there is no direct relationship between state and transaction it would have been straight forward if there was. What i want to do is Display the state name for each transaction in the transaction Gridview index.php

is three solutions:
a) in search query add join with status table - more complicate sql request;
b) in gridview column use function, what request for each cell status name - wrong. For each cell send request to DB
c) in view get status list and pass to column function by use and label get from status list - very good

 [
                   'attribute' => 'mark_id',
                   'filter' => $markList,
                   'value' => function ($model) use ($markList) {
                       return $markList[$model->mark_id] ?? ' - ';
                   },
               ],

I don’t understand.

How do you want to get state name when there is no relationship between Transactions and States?
They must be something linking them together.

Does Agents has a role to play in this?

But there is a relationship between agents and transactions and transactions has agent_id and the agent has state_id.

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

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

In your gridview, you’ll use agent.state.state_name

Hi @femibiwoye thanks for your response. I already did something like this earlier agent.state.name but i think agent.state.state_name should work. I will try it shortly and get back to you. By the way please i have another question here is the link: Display status selectbox as Active or Not-Active in YII2 Gridview .

I truly appreciate your help.

agent & state are determined by the name you gave the relationship, state_name is a column name in States table

1 Like

Thanks Femi, its been taken care of.