<?php
echo $this->Form->postLink(
'',
array('action' => 'edit',$comment['id'],
'controller'=>'Comments',
),array('class'=>'glyphicon glyphicon-edit','onClick'=>'showDialog()') )?>
这是我点击图标时创建的评论编辑图标 弹出模型对话框。
<div id="overlay" onClick="hideDialog()"></div>
<div id="dialog">
<h2>Edit Comment <span onClick="hideDialog()">×</span></h2>
<?php echo $this->Form->create('Comment',array('enctype'=>'multipart/form-data'));?>
<?php echo $this->Form->input('Comment.comment',array('class'=>'form-control'));
echo $this->Form->end('Save');
?>
</div>
<script>
function showDialog() {
document.getElementById("overlay").style.display = "block";
document.getElementById("dialog").style.display = "block";
}
function hideDialog() {
document.getElementById("overlay").style.display = "none";
document.getElementById("dialog").style.display = "none";
}
</script>
用于编辑评论的对话框,但评论只是作为另一个添加 评论但不编辑。为什么?这是我的控制器中的编辑功能
public function edit($id = null) {
$user_id=$this->Auth->user('id');
$comment_fields=$this->Comment->findById($id);
$comment_id=$comment_fields['Comment']['id'];
$comment = $this->Comment->findById($id);
if (!($user_id == $comment_fields['Comment']['user_id'])) {
$this->Flash->error(__('Unable to edit your post.'));
return $this->redirect(array('action'=>'../posts/view',$comment_fields['Comment']['foreign_id']));
}
if (!$comment)
{
$this->redirect(array('action'=>'../posts/view',$comment_fields['Comment']['foreign_id']));
}
if ($this->request->is(array('post', 'put'))) {
if($this->Comment->id = $id)
{
if ($this->Comment->save($this->request->data)) {
$this->Flash->success(__('Your Comment has been updated.'));
$this->redirect(array('action'=>'../posts/view',$comment_fields['Comment']['foreign_id']));
}
}else{
$this->Flash->error(__('Unable to update your Comment.'));
}
}
if (!$this->request->data) {
$this->request->data = $comment;
}
}
答案 0 :(得分:0)
您正在尝试在if的条件语句部分中分配值!
if($this->Comment->id = $id)
:这是错误的,因为该部分用于检查条件。
问题的解决方案是:
$this->Comment->id = $id;
if ($this->Comment->exists()) {
if ($this->Comment->save($this->request->data)) {
$this->Flash->success(__('Your Comment has been updated.'));
$this>redirect(array('action'=>'../posts/view',$comment_fields['Comment']['foreign_id']));
}
}
else {
$this->Flash->error(__('Unable to update your Comment.'));
}