2012-05-22 20 views
1

創建與表中的字段標籤PHP從型號

echo $this->Form->create('Street'); 
echo $this->Form->input('street', array('empty' => '-- select --', 'label' => '???')); 

DB

ID | Street | Description 
----------------------- 
1 | Foo | Street 1 description 
2 | Bar | Street 2 description 
3 | FooFoo | Street 3 description 

我要創建標籤,如:

Foo - Street 1 description 

像:

echo $this->Form->input('street', array('empty' => '-- select --', 'label' => 'Street.street - Street.description')); 

我如何使用CakePHP Form Helper生成這個?謝謝!

回答

0

我不明白你爲什麼想這樣做,但這取決於你想要做什麼。如果您處於添加操作中,顯然您無法從數據庫讀取數據,因爲它中沒有這樣的記錄。 如果您處於編輯操作中(我假定它已被烘焙),則視圖中有數據。所以,你可以做到以下幾點:

在控制器應該是這樣的:

//There should be a variable called $street containing the record data for this to work 
//The following sets $street, so it is accessible as $street in the view 
$this->set(compact('street')); 

在視圖:

echo $this->Form->input('street', array('empty' => '-- select --', 'label' => $street['Street']['description'])); 

如果有很多記載,$street陣列將被索引。

+0

您好! '$ street ['Street'] ['description']'因爲數組索引而無法工作,如果我有多條街道。 –

+0

遍歷視圖中的數組以獲得正確的索引... –