Display the state name for each transaction in the transaction Gridview index.php


(chinaka) #1

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’]);
}

(Alexander Makarov) #2

So what’s the problem?


(Femi) #3

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.


(chinaka) #4

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


(chinaka) #5

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


(Uldisnelsons) #6

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

(chinaka) #7

I don’t understand.


(Femi) #8

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?


(chinaka) #9

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


(Femi) #10

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


(chinaka) #11

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.


(Femi) #12

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


(chinaka) #13

Thanks Femi, its been taken care of.