现在我的选择框是从我的Players
类中的一个虚拟字段创建的,用于我的模型的add.ctp
视图中。Statistics
选择框当前正在按 id 排序。我宁愿它按Player.last_name
.
这是我的模型:
class Player extends AppModel {
public $name = 'Player';
public $virtualFields = array(
'name' => "TRIM(CONCAT(Player.last_name, ', ', Player.first_name))"
);
public $displayField = 'name';
var $hasMany = array(
'Statistic' => array(
'className' => 'Statistic',
'foreignKey' => 'player_id',
'dependent'=> true
)
);
}
这就是我在我add.ctp
看来使用的:
echo $this->Form->input('player_id');
这是当前输出(按 player_id 排序):
<select name="data[Statistic][player_id]" id="StatisticPlayerId">
<option value="335">Smith, Jordan</option>
<option value="336">Marall, Jabari</option>
<option value="337">Rusell, Mike</option>
<option value="338">Johnson, Calvin</option>
<option value="339">Thompson, Daniel</option>
<option value="340">Dennis, Jacob</option>
</select>
这是所需的输出(按姓氏排序):
<select name="data[Statistic][player_id]" id="StatisticPlayerId">
<option value="340">Dennis, Jacob</option>
<option value="338">Johnson, Calvin</option>
<option value="336">Marall, Jabari</option>
<option value="337">Rusell, Mike</option>
<option value="335">Smith, Jordan</option>
<option value="339">Thompson, Daniel</option>
</select>