2012-08-28 31 views
0

我遇到了一個我無法解決的問題。牆腳本返回評論(FuelPHP)

我創建了一個類似於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; 
    }); 

首先,我不希望任何人來寫這對我來說,我只是想問問有經驗的開發人員能夠通過我的代碼看,給我一些提示有關返回的意見邏輯。

回答

1

當你有流posts數組時,你可以從中獲取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; 
} 
+0

謝謝你的回覆我會盡力 – Side

+0

謝謝你的幫助sir – Side

0

您應該使用ORM在帖子之間建立關係並收集它的相關注釋。這就是我會做的。