Simple question by a cakephp noob:
i have two models, Player and Team.
Team has an id (int) and a cool_name (varchar).
Player has an id (int), a cool_name (varchar) and a reference for the team table, team_id (int).
Cool_name instead of name because i don't have tables in english, so i can't use the field 'name'.
So, a team hasMany players and a player belongsTo team.
My Player model:
class Player extends AppModel {
var $name = 'Player';
var $belongsTo = array('Team');
}
(Team model has nothing other than name inside)
PlayersController:
class PlayersController extends AppController {
var $name = 'Players';
function add() {
$this->set('teams', $this->Player->Team->find('list'));
//then save...
}
In the add view of the player:
//...
echo $form->input('squadra_id');
//...
Ok so i have a select with the team ids inside; i don't want the id, but the name of the team and then save the id: something like (in html)
<select name="data[Player][team_id]" id="PlayerTeamId">
<option value="1">Cool Name 1</option>
<option value="2">Cool Name 2</option>
<!-- -->
<option value="{team id}">{team cool name}</option>
</select>
How can i do?
- Solution -
instead of
$this->set('teams', $this->Player->Team->find('list'));
put this
$this->set('teams', $this->Player->Team->find('list', array('fields' => array('cool name') ) ) );