2016-01-06 13 views
0

我想從表中編輯CakePHP 3中的主鍵WTEL手錶在CakePHP中編輯主鍵3

主鍵字段默認情況下在視圖中隱藏,所以我使用此代碼行,使之出現在文件src/Template/Watch/edit.ctp中(由腳手架生成):

echo $this->Form->input('WTEL', array('type' => 'text')); 

WTEL出現字段,但是當我修改輸入WTEL並點擊提交按鈕WTEL值不被修改。

但是此代碼行爲添加頁面用於添加WTEL值。

回答

0

爲了澄清,您希望能夠在edit.ctp視圖中更新現有行的主鍵(使用Watch Controller中的缺省烘焙編輯功能)?

要做到這一點是做一個「複製」功能,最簡單的辦法,東西沿着這些線路:

public function copy($id = null){ 

    //get the old Watch to be copied 
    $oldWatch = $this->Watch->get($id); 

    $watchTable = TableRegistry::get('Watchs'); 
    $watch= $watchsTable->newEntity(); 

    //Copy all existing values into new row, e.g. 
    $watch->description = $oldwatch->description; 

    if ($watchsTable->save($watch)) { 
     // The $watch entity contains the id now, so you are returned 
      to the edit page for the newly created 'watch' 
     $id = $watch->id; 
    } 

    return $this->redirect(['action' => 'edit', $id]); 
} 

如果你想創建一個自定義主鍵,你可以嘗試添加此:

$watch->id = $this->data['Watch']['id']; 

在上面評論的'複製所有現有值'部分。

這是否回答你的問題?

+0

是的,這正是我想要做的!如果我理解的很好,我必須在WatchController中創建一個複製功能。要調用這個函數,我必須將鏈接替換爲要複製的鏈接(我將在之後重定向到編輯頁面)。我必須創建一個copy.ctp。 –

+0

我在函數中做了一些小改動,比如我的表叫做Watch(我知道我應該叫它Watch),所以我改變了:TableRegistry :: get('Watchs')給TableRegistry :: get('Watch');和$ watchsTable到$ watchTable。但是我得到兩個錯誤:注意(8):未定義變量:oldwatch [APP/Controller/WatchController.php,第104行] 注意(8):嘗試獲取非對象的屬性[APP/Controller/WatchController.php, 104行] 感謝您的幫助;) –