尝试将url'/ localhost / topp / events / view / 14'中的当前事件ID'14'保存到comments表中的event_id字段。我在评论控制器中使用了add函数,在events view.ctp页面上使用了“添加注释”按钮。有任何想法吗?我被建议使用隐藏的id字段和$ this-> params ['pass'] ['0']但不确定如何解决这个问题,欢迎任何建议。提前谢谢。
我当前的评论控制器添加操作代码:
public function add() {
//$this->loadModel('Event');
if ($this->request->is('post')) {
$this->Comment->create();
$this->request->data['Comment']['user_id'] = $this->Auth->user('id');
//Commented below is the logic Iam trying to achieve but is wrong
//$this->request->data['Comment']['event_id'] = $this->request->data['Event']['id'];
if ($this->Comment->save($this->request->data)) {
$this->Session->setFlash(__('The comment has been saved'));
$this->redirect(array('controller' => 'events', 'action' => 'myevents'));
} else {
$this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
}
}
}
////////////////////////////////////////我的活动查看代码并添加评论链路//////////////////////////
<div class="events view">
<?php echo $this->element('maintitle'); ?>
<?php echo $this->element('profile_area'); ?>
<h2><?php echo __('Event'); ?></h2>
<dl>
<td><?php echo $this->Html->image('/files/event/photo/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo'], array("alt" => "No Event Image")); ?> </td>
<td><?php echo $this->Html->image('/files/event/photo2/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo2'], array("alt" => "No Event Image")); ?> </td>
<td><?php echo $this->Html->image('/files/event/photo3/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo3'], array("alt" => "No Event Image")); ?> </td>
<td><?php echo $this->Html->image('/files/event/photo4/'.$event['Event']['id'].'/thumb_'.$event['Event']['photo4'], array("alt" => "No Event Image")); ?> </td>
<dt></dt>
<dd>
<br>
</dd>
<dt><?php echo __('Name'); ?></dt>
<dd>
<?php echo h($event['Event']['name']); ?>
</dd>
<dt><?php echo __('Date'); ?></dt>
<dd>
<?php echo h($event['Event']['date']); ?>
</dd>
</dl>
</div>
<div class="related">
<h3><?php echo __('Related Comments'); ?></h3>
<?php if (!empty($event['Comment'])): ?>
<?php echo ('Add a comment for this event') ?>
<?php
$i = 0;
foreach ($event['Comment'] as $comment): ?>
<tr>
<td><?php echo $comment['comment']; ?></td>
<td class="actions">
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<div class="actions">
<ul>
<li><?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?> </li>
</ul>
</div>
</div>
答案 0 :(得分:1)
让我看看我是否做对了。
右?
然后,您需要将事件的id从event_view.ctp(我在这里发明名称)传递给comment_add.ctp。
有两种方法可以做到这一点
第一种方式:在链接中传递事件的ID并在此类操作中接收
//event_view.ctp
//I'm going to assume you know the event id here
$this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add', $event_id)); ?>
这将创建一个类似 comments / add / 14 的链接,例如,其中14是事件ID。现在在操作中,您会收到ID
//pass it like parameter of the action
public function add($event_id = null) {
if ($this->request->is('post')) {
$this->Comment->create();
$this->request->data['Comment']['user_id'] = $this->Auth->user('id');
//check that the event_id isn't null and that the event actually exists first
$this->request->data['Comment']['event_id'] = $event_id;
/*etc*/
}
}
您无需将event_id传递给comment_add视图,如果用户不需要处理它,则无需将其添加到表单中。
第二种方式:如果你不想在url中传递event_id,你需要通过POST传递。为此,您现在必须添加新注释的链接需要更改为隐藏了event_id的表单。
echo $this->Form->create('Comment', array('url'=>array('controller' => 'comments', 'action' => 'add')));
echo $this->Form->input('event_id', array('type'=>'hidden', 'value'=>$event_id);
echo $this->Form->end('New comment');
//instead of <?php echo $this->Html->link(__('New Comment'), array('controller' => 'comments', 'action' => 'add')); ?>
在评论的操作中,检查您收到的帖子是否包含event_id,但不包含user_id。我发现这种方式有点混乱,因为你收到的每个请求都是一个帖子,所以当你想要显示要填充的表单时,以及当你想要保存它时,就很难区分。
我更喜欢第一种方式,我建议这样做。但我觉得有必要指出还有其他方法可以做到这一点。取决于你想要的东西。