CakePHP - 存储外键

时间:2012-10-12 15:13:42

标签: cakephp

我有三种模式:

  1. 用户
  2. 注意
  3. 注释
  4. 用户可以有多个备注(通过笔记我的意思是像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?

2 个答案:

答案 0 :(得分: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');

应用/控制器/ CustomersController.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);

    }
}

答案 1 :(得分:0)

CONTROLLER:

公共功能notesview()       {

 $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;
 ?>