发表评论CakePHP 3

时间:2016-07-20 17:06:57

标签: database cakephp

我是CakePHP的新手,我需要开发一个用户登录并发表评论的系统,但我不知道如何获取他的id并从'comments'中插入user_id FK。有人能帮助我吗?

添加功能 - ComentarioController.php

 public function add()
    {
        $comentario = $this->Comentario->newEntity();
        if ($this->request->is('post')) {
            $comentario = $this->Comentario->patchEntity($comentario, $this->request->data);
            if ($this->Comentario->save($comentario)) {
                $this->Flash->success(__('Comentário enviado com sucesso'));

                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('Erro no envio do comentário'));
            }
        }
        $this->set(compact('comentario'));
        $this->set('_serialize', ['comentario']);
    }

add.ctp

<nav class="large-3 medium-4 columns" id="actions-sidebar">
    <ul class="side-nav">
        <li class="heading"><?= __('Actions') ?></li>
        <li><?= $this->Html->link(__('Listar Comentarios'), ['action' => 'index']) ?></li>
    </ul>
</nav>
<div class="comentario form large-9 medium-8 columns content">
    <?= $this->Form->create($comentario) ?>
    <fieldset>
        <legend><?= __('Postar Comentario') ?></legend>
        <?php


            echo $this->Form->input('comentario');
        ?>
    </fieldset>
    <?= $this->Form->button(__('Comentar')) ?>
    <?= $this->Form->end() ?>
</div>

1 个答案:

答案 0 :(得分:0)

如果用户已登录,您可以使用$this->Auth->user()AuthComponent::user()获取其ID 如果您还没有登录系统,请尝试CakeDC/Users plugin,这通常应该是开箱即用的,并且帮了我很多次。