0

我有三个模型:

  1. 用户
  2. 笔记
  3. 评论

一个用户可以有多个注释(我的注释是指像 Facebook 这样的状态),一个注释可以有多个评论。

在评论表中,我想存储实际上是外键的 user_id 和 note_id。

评论控制器:

public function userscomment()
{
    if (!empty($this->data))
    {
        $commentdata = $this->data;
        $commentdata['Comment']['user_id'] = $this->Auth->user('id');
        $this->Comment->save($commentdata);
    }

现在用户 id 很容易识别,它存储在数据库中,但是我如何识别评论是针对这个注释的呢?

如何存储发表评论的便笺的 id?

4

2 回答 2

0

在您的视图中,您可以在其中输入评论的表单,您可以指定一个表单操作,将注释 ID 发送到您的控制器,如下所示:

应用程序/查看/评论/userscomment.ctp

echo $this->Form->create('Comment', array(
    'url' => array('385')  // 385 is the note_id
));
echo $this->Form->textarea('comment');

echo $this->Form->end('Submit Comment');

应用程序/控制器/客户控制器.php

public function userscomment($note_id) {

    if (!empty($this->data)) {

        $commentdata = $this->data;
        $commentdata['Comment']['user_id'] = $this->Auth->user('id');
        $commentdata['Comment']['note_id'] = $note_id;
        $this->Comment->save($commentdata);

    }
}
于 2012-10-12T16:09:13.267 回答
0

控制器:

公共功能注释视图(){

 $allnotes = $this->User->Note->find('all', array('order'=>'Note.created DESC'));


    if (!empty($this->params['requested'])) 
      {            
        return $allnotes;        
      } 
  }

看法:

<?php 
echo $this->Html->css('notes'); 
echo $this->Html->script(array('jquery','jquery.elastic','notes'));
?>
<div class="notescontainer">
<div class="share">Share:</div>
<div class="notes">Notes</div>
<div class="loading"></div>
<div class="img_top"></div>
<div class="text_status">

<?php 
echo $this->form->create('Note', array('action' => 'notes', 'onSubmit' => 'return loadingicon();'));
echo $this->form->input('notes',array('class'=>'notesbar','label'=>'','placeholder'=>'Write down you notes ...' ));
?>
 </div>
<div class="button_outside_border" id="share">
<div class="button_inside_border">

<?php 
echo $this->form->submit('Share',array('class' => 'sharebutton'));
echo $this->form->end();
?>
</div>
</div>
<div class="clear"></div>
</div>

<?php 

$allnotes = $this->requestAction(array('controller'=>'Users', 'action'=>'notesview'));
foreach($allnotes as $viewnotes):
{
    echo "<div class='load_status'>"; 

    echo "<img class='status_img' src="; echo $viewnotes['User']['image']; echo ">"; 

    echo "<div class='status_text'>" ?>

    <a href="users/profileview/<?php echo $viewnotes['User']['id']?>" class='blue'> 
    <?php
    echo $viewnotes['User']['name'];
    echo "</a>";

    echo "<p class='text'>"; 
    echo $viewnotes['Note']['notes']; 
    echo "</p>"; 


    echo " 
    <a href='#' class='light_blue'>Like</a> &middot; 
    <a href='#' class='light_blue'>Comment</a> &middot ";
    echo "<u class='date'>"; echo $viewnotes['Note']['created']; echo"</u>";

    echo $this->element('comments');

    echo "</div>";


    echo "<div class='clear'></div>";
    echo "</div>";


}
endforeach;
 ?>
于 2012-10-12T17:06:21.933 回答