Yii2简单就像保存在数据库中的不喜欢按钮

时间:2017-04-11 13:10:57

标签: php mysql yii2

这项任务我差不多两天都在挣扎!我看了很多帖子和视频,但没有意识到怎么做。我到目前为止的地方是like_id | user_id | post_id column的表格,like_id是主键,user_id是喜欢评论的用户的外键,而post_id是与该评论相关的帖子的外键。我想我可以阻止用户多次喜欢。

我的行动评论是:

public function actionComments($id)
    {
        $comments = \Yii::$app->db->createCommand("SELECT *
                                                        FROM post_comments
                                                        WHERE post_id=$id
                                                        ORDER BY comment_id
                                                        DESC ")->queryAll();

        $likes = \Yii::$app->db->createCommand("SELECT *
                                                     FROM likes")->queryAll();

        if(\Yii::$app->user->isGuest)
        {
            return $this->redirect(['../site/error', '405', 'You have to be loged in, to see this content!']);
        }

        return $this->render('comments',[
            'comments' => $comments,
            'likes' => $likes
        ]);
    }

我的评论观点:

<?php

use yii\helpers\Html;
use app\models\BlogUser;
use app\controllers\PostController;
?>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <?php foreach ($comments as $comment): ?>
                <div class='col-md-3 post-prof-img'>
                    <?php
                        $currUser = BlogUser::find()->where(['id' => $comment['author_id']])->one();
                    ?>
                    <?= Html::img('../images/' . $currUser->image, ['class' => 'tall img-circle']) ?>
                    <p class="text-center title-par">
                        <em><strong><?= $currUser->username ?></strong></em>
                    </p>
                </div>
                <div class='col-md-9 col-md-offset-1'>
                    <div class='post-comment'>
                        <p><em><?= $comment['comment_content'] ?></em></p>
                    </div>
                    <div class='comment-options'>
                        <div class='col-md-8'>
                         <?php
                             if(\Yii::$app->user->identity->id == $comment['author_id'] || PostController::isAdmin())
                             {
                                 echo Html::a('Edit',['update-comment', 'id' => $comment['comment_id']]);
                                 echo Html::a('Delete',
                                     ['delete-comment', 'id' => $comment['comment_id']],
                                     ['data' => ['method' => 'POST']]);
                             }
                             echo Html::a('Like');
                             echo Html::a('Dislike');
                          ?>
                        </div>
                        <div class='col-md-4 text-right'>
                            <div class="ajax-helper">
                                <span class='glyphicon glyphicon-hand-up' style="color:green"></span>
                                   <!--LIKES COUNT-->
                                <span class='glyphicon glyphicon-hand-down' style="color:red"></span>
                                    <!--DISLIKES COUNT -->
                            </div>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
            <div>
                <?= Html::a('Add Comment', ['create-comment', 'id' => $_GET['id']],['class' => 'btn btn-primary add-comment', 'style'=>'margin-top: 2%']) ?>
            </div>
        </div>
    </div>
</div>

我知道在这种情况下我必须使用ajax!我一团糟!我求求一些建议。不是一个完整的答案,只是建议!提前谢谢!

1 个答案:

答案 0 :(得分:1)

使用pjax进行动态输出。编写代码,以便在显示喜欢/不喜欢按钮之间进行选择。你可能需要一个带PK的表,post_id,user_id,比如

控制器/行动:

public function actionTestPjax1($post_id = null, $like = null) {
    // try get like per post
    if (!empty($post_id) && !Yii::$app->user->isGuest) {
        $data = Like::find()->where(['user_id' => Yii::$app->user->id, 'post_id' => $post_id])->one();

        if (!empty($like)) {
            // set from like /dislike
            if (empty($data)) {
                // new record
                $data = new Like;
                $data->post_id = $post_id;
            }
            $data->like = $like;
            $data->save();
        } else {
            // we want to get the current state
            if (!empty($data)) {
                $like = $data->like;
            }
        }
    } else {
        // error
    }
    return $this->render('test-pjax1', ['like' => $like, 'time' => time()]);
}

查看test-pjax1:

<?php
use \yii\widgets\Pjax;
use yii\helpers\Html;
?>

timestamp <code><?= $time ?></code>
<hr>

<?php Pjax::begin([
    'id'                 => 'pjax1',
    'enablePushState'    => false,
    'enableReplaceState' => false,
    'linkSelector'       => '#pjax1 a',
    'timeout'            => 10000,
]) ?>

timestamp <code><?= $time ?></code> (in pjax container)

<?php if (empty($like)) { ?>

<hr>
<?= Html::a('LIKE', [site/test-pjax1', 'like' => 1, ]) ?>
 / 
<?= Html::a('DISLIKE', ['site/test-pjax1', 'like' => -1, ]) ?>

<?php } else { ?>

    <hr>
    <p>
        <?= ($like == 1) ? 'you like me :)' : 'you do not like me :('; ?>
    </p>

<?php } ?>


<?php Pjax::end() ?>