因此,我正在學習CakePHP,並且我試圖構建一堆基本的應用程序,我可以使用直接構建的PHP來學習CakePHP的工作方式。現在我正在處理基本的訂單管理系統 - 用戶可以有多個訂單和訂單屬於用戶。在CakePHP中更新記錄
我有數據庫設置和CakePHP配置正確(起始頁顯示每個主題的所有綠色,如默認時區和數據庫連接,除了我沒有安裝的DebugKit)。我也有一個「用戶列表」頁:
我運行到,你可以在上面的截圖實際看到的問題是,當我試圖在編輯現有用戶/用戶/ edit_user:
我試圖保存編輯對於用戶來說,和I(1)使用$this->Form->hidden(...)
用於ID和(2)中設置的主鍵表中的主鍵中型號有public $primaryKey = 'ID';
我每次嘗試這樣做時,都會插入一條記錄,而不是更新現有記錄,並且我認爲通過識別我的PK並添加要提交的ID可以解決問題,但我必須遺漏了什麼。
下面是所涉及的頁面代碼:
型號
<?php
/**
* app/Model/User.php
*
*/
class User extends AppModel
{
public $name = 'User';
public $primaryKey = 'ID';
// <hasMany>
public $hasMany = array(
'Order' => array('dependent' => true)
);
}
?>
控制器
<?php
/**
* app/Controller/UsersController.php
*
*/
class UsersController extends AppController
{
//public $scaffold;
/* Validation */
public $validate = array(
'username' => array(
'required' => true,
'allowEmpty' => false,
'loginRule1' => array(
'rule' => 'alphaNumeric',
'message' => "Usernames must be alphanumeric!"
),
'loginRule2' => array(
'rule' => array('minLength', 4),
'message' => 'Usernames must be at least 4 characters.'
),
),
'password' => array(
'required' => true,
'allowEmpty' => false,
'passwordRule1' => array(
'rule' => array('minLength', 4),
'message' => 'Passwords must be between 4 and 8 characters.'
),
'passwordRule2' => array(
'rule' => array('maxLength', 8),
'message' => 'Passwords must be between 4 and 8 characters.'
)
)
);
/* Actions */
public function index()
{
// Grab all users
$users = $this->User->find('all');
// Set/Send users to View
$this->set('users', $users);
}
public function new_user()
{
/* Has any Form data been POST'd? */
if ($this->request->is('post'))
{
// Save User data to the DB
$this->User->save($this->request->data);
$this->redirect('/users');
}
}
/*
Accessing Args:
(1) Direct URL Routing Method
--> ...
(2) Named Params Method
--> CakeRequest['named']['[name]']
*/
public function edit_user($id = null)
{
$id = $this->request['named']['currentID'];
// Display the current data of the user
if (!empty($id))
{
$userInfo = $this->User->find('all',
array('conditions' => array('User.ID' => $id)));
$this->set('userData', $userInfo);
}
else
$this->set('error', '$id is empty!');
/* Has any Form data been POST'd? */
if ($this->request->is('post'))
{
// Save User data to the DB
$this->User->save($this->request->data);
$this->redirect('/users');
}
}
}
?>
查看/用戶/ edit_user.ctp
<div class="row-fluid">
<div class-"span12"></div>
<div class="span12"></div>
<div class="span8 offset2">
<?php
echo "Hidden ID Field Value: " . $userData[0]['User']['ID'];
echo "<hr>";
echo $this->Form->create('User');
echo $this->Form->input('username');
echo " Current Name: " .
$userData[0]['User']['username'];
echo $this->Form->input('password');
echo " Current password: " .
$userData[0]['User']['password'];
// Hidden ID field to make save ~ an UDATE
echo $this->Form->hidden('User',
array('ID' => $userData[0]['User']['ID'],
'type' => 'hidden'));
echo $this->Form->end('Save');
?>
</div>
</div>
有誰看到什麼我加入這樣做會允許我編輯 /更新現有的記錄,而不是插入一個全新的? 感謝您的時間!
不知道它是否會有所作爲......但是當我使用ID輸入字段來實現這樣的功能時,我傾向於使用類似這樣的形式:'echo $ this-> Form-> input('id', array('type'=>'hidden'));' – summea 2013-03-19 21:32:30
就是這樣! 謝謝你糾正我的n00b錯誤。 :) – 2013-03-19 21:47:00