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 yiihelpersHtml;
use appmodelsBlogUser;
use appcontrollersPostController;
?>
<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!我一团糟!我乞求一些建议。不是一个完整的答案,而只是建议!提前谢谢你!

使用 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()]);
}

查看测试-pjax1:

<?php
use yiiwidgetsPjax;
use yiihelpersHtml;
?>
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() ?>

最新更新