kasambo
(Kasambo)
1
Hi.
I have three tables; Player,Team and Team_Player.
Team_Player is a junction table.
Player (Player_ID, Player_First_Name, Player_Last_Name)
Team (Team_ID, Team_Name, Team_Location)
Team_Player (Player_ID, Team_ID, Player_Start_Date, Player_Salary)
The relations have been set up properly with via table.
I need to create a dropdownlist of all the Players with full names associated with a particular Team.
Please Help.
softark
(Softark)
2
Basically something like this will do:
$team = Team::find()->with("players")->findOne($team_id);
$items = []
foreach($team->players as $player) {
$items[$player->id] = $player->first_name . ' ' . $player->last_name;
}
echo Html::activeDropDwnList($model, 'attribute', $items);
kasambo
(Kasambo)
3
Thank You Softark.
I tried the code but, it gives me this error: Calling unknown method: yii\db\ActiveQuery::findOne()
alderson
(Ald3rson)
4
where did you put that above code? put that in model
softark
(Softark)
5
Oh!!
I’m very sorry.
$team = Team::find()->with("players")->[s]findOne/s;
$team = Team::find()->with("players")->One($team_id);
Calcio
(Cálcio)
6
@Bumbuli, it’s recommended do the query on the model, in the view only activeDropDwnList()
kasambo
(Kasambo)
7
Yes, I have put in the Team model.
Then I get:
Call to a member function getQueryBuilder() on a non-object
kasambo
(Kasambo)
8
Yes, I have put in the Team model.
Then I get:
Call to a member function getQueryBuilder() on a non-object
kasambo
(Kasambo)
9
Thank You.
Yes, I have put in the Team model.
Then I get:
Call to a member function getQueryBuilder() on a non-object
kasambo
(Kasambo)
10
I might be doing something wrong!!
kasambo
(Kasambo)
11
Okay,
I have done some changes on the code to:
$team = Team::find($team_id)->with("players")->One();
Now this throws:
Getting unknown property: frontend\models\Team::Player_ID
kasambo
(Kasambo)
12
Thank you all,
I have found the solution.
LearnYii2
(Timeshift11)
13
Hey, this was getting interesting. Can you please tell us the solution you found?
Thanks.
kasambo
(Kasambo)
14
//All my field are on this Format: TEAM_PLAYER_ID, TEAM_ID, PLAYER_ID (i.e. Caps and Underscore)
//I put everything on Capital letters.
// I put this code on team model
public function getTeamMembers()
{
///// Set TEAM_ID
if(isset(Yii::$app->session[‘TEAM_ID’]))
{
$TEAM_ID = \Yii::$app->session->get('TEAM_ID');
}
//////
$teams = Team::find()
->with('teamPlayers','teamPlayers.pLAYER')
->where(['team.TEAM_ID' => $TEAM_ID])
->all();
$items = [];
foreach($teams as $team)
{
$teamplayers = $team->teamPlayers;
foreach($teamplayers as $team_player)
{
$players = $teamplayers->pLAYER;
$PLAYER_ID = $players->PLAYER_ID;
$PLAYER_LAST_NAME = $players->PLAYER_LAST_NAME;
$PLAYER_FIRST_NAME = $players->PLAYER_FIRST_NAME;
foreach($players as $player)
{
$items[$PLAYER_ID] = $PLAYER_LAST_NAME . ' ' . $PLAYER_FIRST_NAME;
}
}
}
return $items;
}
//THEN on the form I have:
$items = $team->getTeamMembers();
echo "<label>Player</label> : ";
echo Html::activeDropDownList($player, ‘PLAYER_ID’, $items);