所以我正在学习 CakePHP,并且我正在尝试构建一堆可以直接用 PHP 构建的基本应用程序,以了解 CakePHP 的工作原理。现在我正在开发一个基本的订单管理系统——用户可以有多个订单并且订单属于一个用户。
我已经设置了数据库并且正确配置了 CakePHP(起始页面显示所有主题的所有绿色,例如默认时区和数据库连接,但我尚未安装的 DebugKit 除外)。我还有一个“用户列表”页面:
正如您在上面的屏幕截图中实际看到的那样,我遇到的问题是当我尝试在 /users/edit_user 上编辑现有用户时:
我正在尝试为用户保存编辑,我 (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>
有没有人看到我没有做这将允许我编辑/更新现有记录而不是插入一个全新的记录?谢谢你的时间!