【问题标题】:CakePHP - Storing foreign keysCakePHP - 存储外键
【发布时间】:2012-10-12 15:13:42
【问题描述】:

我有三个模型:

  1. 用户
  2. 注意
  3. 评论

一个用户可以有多个便笺(我的便笺是指像 Facebook 这样的状态),一个便笺可以有多个 cmets。

在评论表中,我想存储实际上是外键的 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?

【问题讨论】:

  • 您的页面上通常会有一个隐藏的表单输入,其注释 id 为:<input type="hidden" name="data[Comment][note_id]" value="4" />
  • 你能不能说的清楚一点,我没听明白

标签: cakephp


【解决方案1】:

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

app/View/Comments/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');

app/Controller/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);

    }
}

【讨论】:

  • 这究竟是什么问题呢。在这里您手动输入了 id,但我不知道如何自动调用 id。就像每个笔记都有一个唯一的 id 一样,当我们通过 Auth 组件调用用户 id 时如何调用它。
  • 好的,在某个地方你必须知道视图中的笔记 ID,不是吗?否则如何显示已经存在的注释或 cmets?你能发布你的视图 HTML 让我看看吗?
  • 检查下面的视图和控制器。
【解决方案2】:

控制器:

公共函数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;
 ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多