0

我遇到了一个我无法解决的问题。

我创建了一个类似于 Facebook 的墙,一切正常。我从来没有这样做过以这种方式返回评论;我对如何将评论返回到流帖子一无所知。

这是我的代码

该模型

class Main extends \Model {

    static function post_stream()
    {
        DB::set_charset('utf8');
        $query = DB::insert('stream_post');

        $query->set(array(
            'stream_text' => \Input::post('stream_post'),
            'user_id' => \Session::get('sentry_user'),
            //'date' => time(),
        ));

        $query->execute();
    }

    static function get_stream()
    {
        $query = DB::select()->from('stream_post');
        $query->join('users_metadata');
        $query->on('stream_post.user_id', '=', 'users_metadata.user_id');
        $query->order_by('stream_post.stream_id', 'DESC');
        $result = $query->execute();
        foreach($result as $row)
        {
            $data[] = $row;
        }
        return $data;
    }

    static function post_stream_comment()
    {
        $query = DB::insert('stream_comment');

        $query->set(array(
            'user_id' => \Session::get('sentry_user'),
            'stream_id' => \Input::post('comment_post'),
            'comment_text' => \Input::post('comment_text'),
            )
        );

        $query->execute();
    }

查看(我用 jQuery 加载这个)

<div class="stream-posts">
    <?php foreach($posts as $post): ?>
        <div id="post_<?php echo $post['stream_id']; ?>" class="span6 stream-content">
            <span class="user"><a href="#"><?php echo $post['full_name']; ?></a></span>
            <p><?php echo Input::auto_link(Input::nl2br_limit($post['stream_text'], 2)); ?></p>
            <div class="clear"></div>
            <div class="feedback">
                <a href="#" class="like" rel="nofollow">Tetszik</a> - 
                <a href="#" class="comment" rel="nofollow">Hozzászólás</a>
            </div>
            <div class="clear"></div>
            <div class="stream-comment">
                <form class="stream-comment-form">
                    <input type="hidden" name="comment_post" value="<?php echo $post['stream_id']; ?>">
                    <textarea name="comment_text"></textarea>
                    <button type="submit" class="btn btn-primary btn-mini pull-right">Hozzászólok</button>
                </form>
            </div>
        </div>
        <div class="clear"></div>
    <?php endforeach; ?>
</div>

控制器

function action_loadstream()
    {

        $data['posts'] = Main::get_stream();
        $data['comments'] = Main::get_stream_comment();
        return View::forge('main/loadstream', $data);
    } 

    function action_post_stream()
    {

        Main::post_stream();
        $data['get'] = Main::get_stream();
        $stream = json_encode(
            array(
                'full_name' => $data['get'][0]['full_name'],
                'stream_text' => Input::auto_link(Input::nl2br_limit($data['get'][0]['stream_text'], 2)),
                'stream_id' => $data['get'][0]['stream_id'],
                )
            );
        return $stream;
    }

jQuery

//stream load
        $('#stream-load').load(site_url + "main/loadstream", function(){
            //oembed();
            $('.stream-comment textarea').elastic();
    }); 

//stream comment send
            $('.stream-comment-form').on('submit', function(){
                var commentData = $(this).serialize();
                $.ajax({
                    type: "post",
                    url: site_url + "main/post_stream_comment",
                    data: commentData,
                    success: function()
                    {
                        alert('yay');
                    }
                });
                return false;
            });

        }, 100000).hide().fadeIn("slow");


    function embedHtml(stream)
    {
        var html = '';
            html += '<div class="span6 stream-content">';
            html += '<p class="stream-user"><a href="#">'+stream.full_name+'</a></p>';
            html += '<p>'+stream.stream_text+'</p>';
            html += '<div class="clear"></div>';
            html += '<div class="feedback">';
            html += '<a href="">Tetszik</a> - ';
            html += '<a href="#" class="comment">Hozzászólás</a>';
            html += '</div>';
            html += '<div class="clear"></div>';
            html += '</div>';
            html += '<div class="clear"></div>';
        return html;
    }

    //stream post send
    $('#stream-form').on('submit', function(){
        var streamData = $(this).serialize();
        $('#stream-submit').attr('disabled', 'disabled');
        $.ajax({
            type: "post",
            url: site_url + "main/post_stream",
            data: streamData,
            dataType: "json",
            success: function(stream)
            {

                $('#stream-submit').removeAttr('disabled', 'disabled');
                $('#stream textarea').val('');
                $('#stream-load').prepend(embedHtml(stream));
                $('.stream-posts:first-child').hide().slideDown("slow");
                //oembed();
            }
        });
        return false;
    });

首先,我不想让任何人为我写这个,我只是想请一位更有经验的开发人员查看我的代码并给我一些关于返回评论逻辑的提示。

4

2 回答 2

1

当您拥有流帖子数组时,您可以从中提取 id,然后对数据库进行一次查询,以获取所有相关评论。循环遍历它,并将其附加到 post 对象/数组。它类似于 ORM 急切加载模式。所以:

// get all the post ids
$post_ids = Arr::pluck($result, 'id'); // since 1.3

// fetch all relating comments in one query
$comments = DB::select()
    ->from('stream_comments')
    ->where('post_id', 'in', $post_ids)
    ->execute()
    ->as_array();

// attach them to the original result set.
foreach($comments as $comment)
{
    is_array($result[$comments['post_id']]) or $result[$comments['post_id']] = array();

    $result[$comments['post_id']][] = $comment;
}
于 2012-08-28T21:56:10.023 回答
0

您应该使用 ORM 在帖子之间建立关系并收集它的相关评论。这就是我会做的。

于 2013-03-18T21:36:09.713 回答