2011-09-10 36 views
0

我已經構建了一個CakePHP應用程序,該應用程序可以創建從數據庫中的其他表分配給客戶端和醫生的約會。CakePHP在下拉菜單中顯示來自另一個鏈接表的數據

在約會添加屏幕上,我希望能夠從下拉列表中選擇Doctor名稱(注意名稱由兩個字段組成,名字和姓氏,而且它們也是一個字段,稱爲前綴的標題,例如Mr)。它應該保存的信息是doctor_id,因爲這兩個表格是如何鏈接的!

我已經添加了以下我的看法:

<?php echo $this->Form->input('doctor_id',array('label'=>'<strong>Choose Doctor</strong>'),$doctors); ?>

,然後在我的控制器:

$this->set('doctors', $this->Appointment->Doctor->find('list'));

然而,這造成從數據庫中隨機元素的列表。我怎樣才能使這個工作?所以產生的下拉是這樣的:

<select> 
<option value="1">Mr. John Doe</option> 
<option value="2">Mrs. Jane Doe</option> 
</select> 

回答

2

可以指定你想要的字段通過向find()添加'fields'參數在下拉列表中顯示,並且您可以在Doctor模型中使用virtualField來獲取名稱(或全名)。

doctor.php:

public function __construct($id = false, $table = null, $ds = null) { 
    parent::__construct($id, $table, $ds); 

    // This is for MySQL, change for whatever DB flavour you're using. 
    $this->virtualFields = array(
     'name' => "CONCAT(`{$this->alias}`.`prefix`, ' ', `{$this->alias}`.`firstname`, ' ', `{$this->alias}`.`lastname`)" 
); 
} 

在你的控制器更改您的發現( '名單')於:

$this->set('doctors', $this->Appointment->Doctor->find(
    'list', 
    array(
    'fields' => array('Doctor.name') 
) 
); 

當然,你可以添加一個 '秩序' 參數...

$this->set('doctors', $this->Appointment->Doctor->find(
    'list', 
    array(
    'fields' => array('Doctor.name'), 
    'order' => array('Doctor.firstname', 'Doctor.lastname') 
) 
); 

希望有所幫助。

1

他們不是隨機的。

,但你想在你的find()方法使用「命令」 =>陣列(「身份證」 =>「ASC」),以便通過ID對它們進行排序

相關問題