I'm using Associations to Link Models together in CakePHP.
I have a Player
model that is linked to a Statistic
model using a $hasMany
relationship. So basically one player can have many statistics.
After I set up these relationships, I use cake bake
to generate the controllers and views for both the Player
and Statistic
models.
Everything works fine and works as expected, but when I go to add a statistic using the add template, the player_id
is used as the association.
My question is how do I set up the model to use the player_id
as the association but use something like the player's first and last name as the drop down menu in the add template.
Currently if I go to add a new statistic, I get a drop down box that called "Player" that lists all of the player id's but what I want is for the player's first_name and last_name to be in that drop down box instead of the id. I realize that I can modify the controller or template to accomplish this, but I Want to know if I can do this while setting up the model so that cake bake
can take care of it.
Player Model:
<?php
class Player extends AppModel {
public $name = 'Player';
public $belongsTo = array(
'School' => array(
'className' => 'School',
'foreignKey' => 'school_id'
)
);
public $hasMany = 'Statistic';
}
Statistic Model:
<?php
class Statistic extends AppModel {
public $name = 'Statistic';
public $belongsTo = array(
'Player' => array(
'className' => 'Player',
'foreignKey' => 'player_id'
)
);
}
Players Table
`id|school_id|first_name|last_name|number|position