2013-03-25 48 views
0

我發佈了一個問題,並得到了幫助,但我仍然沒有做到。 林試圖更新在CakePHP 2.3.1.I一排具有類試驗延伸AppModel與$驗證陣列,並且所述控制器(簡化):仍然卡在更新行cakephp

public function editstudent() { 
if($this->request->data) { 
    $this->Test->stuId= $this->data['Test']['stuId']; 
    if ($this->Test->save($this->request->data)) { 
     $this->set('state',1); 
    } 
} 

編輯來自用戶建議後我的觀點:d:

class Test extends AppModel { 
var $name= 'Test'; 
var $primaryKey= 'stuId'; 
public $validate= array(.......); 
} 

我設置$的PrimaryKey中查看,設置$這個 - >測試 - > $的PrimaryKey的控制器,但仍然得到了錯誤:

Database Error Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'PRIMARY' SQL Query: INSERT INTO cakePhp.tests (stuName, stuDoB, stuAdd) VALUES ('Đào Đức Anh', '2013-03-25', 'Kim Liên')

誰能幫助我,我真的ñ EED的幫助下,我被堵在這2天,不能弄明白:(

回答

0

快速嘗試:

嘗試在你的控制器代碼改變$this->data$this->request->data

更多的東西:

底線,你的主鍵是沒有得到設置。

  1. 使用(每CakePHP的書)這句法嘗試:

    public $primaryKey = 'stuId'; //notice "public" instead of "var"

  2. 檢查,找出原因 'studId' 是不是在被保存的數據。

    debug($this->request->data);

  3. 我的假設是,它不是在請求數據 - 如果是這樣,你會被罰款。

add the studId into the form that's being submitted or get theID into the request data any other way you see fit

旁註:

你一直說 「觀點」,但你真的是 「模式」。

1

我認爲你必須以下行

$this->Test->stuId = $this->data['Test']['stuId']; 

改變

$this->Test->id = $this->data['Test']['stuId']; 

,因爲它是包含主鍵值id屬性。