Dropdownlist via table yii2

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.

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);



Thank You Softark.

I tried the code but, it gives me this error: Calling unknown method: yii\db\ActiveQuery::findOne()

where did you put that above code? put that in model

Oh!!

I’m very sorry.

$team = Team::find()->with("players")->[s]findOne/s;

$team = Team::find()->with("players")->One($team_id);

@Bumbuli, it’s recommended do the query on the model, in the view only activeDropDwnList()

Yes, I have put in the Team model.

Then I get:

Call to a member function getQueryBuilder() on a non-object

Yes, I have put in the Team model.

Then I get:

Call to a member function getQueryBuilder() on a non-object

Thank You.

Yes, I have put in the Team model.

Then I get:

Call to a member function getQueryBuilder() on a non-object

I might be doing something wrong!!

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

Thank you all,

I have found the solution.

Hey, this was getting interesting. Can you please tell us the solution you found?

Thanks.

//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);