2014-02-08 66 views
0

嗨拉取數據,我建立了一個登錄系統,現在我想從模型中的查詢被稱爲拉動數據post笨和我新的MVC和笨,我已經打了一個障礙模型

代碼post模型

class Post extends CI_MODEL { 

    function stream($uid, $updated, $limit) { 
     //SELECT * FROM vPAS_Posts_Users_Temp WHERE post_user_id =:id AND post_type !=4 AND post_updated >:updated AND post_updated <:now UNION SELECT u.* FROM vPAS_Posts_Users_Temp u JOIN PAS_Follow f ON f.folw_followed_user_id = u.post_dynamic_pid WHERE u.post_updated >:updated AND post_updated <:now AND ((f.folw_follower_user_id =:id AND f.folw_deleted = 0) OR (u.post_passed_on_by = f.folw_follower_user_id OR u.post_passed_on_by =:id AND u.post_user_id !=:id AND u.post_type =4)) ORDER BY post_posted_date DESC LIMIT :limit 
     $now = microtime(true); 
     $sql = " 
      SELECT 
       * 
      FROM 
       vPAS_Posts_Users_Temp 
      WHERE 
       post_user_id = ? 
       AND post_type !=4 
       AND post_updated > ? 
       AND post_updated < ? 
      UNION 

      SELECT 
       u.* 
      FROM 
       vPAS_Posts_Users_Temp u 
      JOIN 
       PAS_Follow f 
       ON f.folw_followed_user_id = u.post_dynamic_pid 
       WHERE u.post_updated > ? 
       AND post_updated < ? 
       AND ((f.folw_follower_user_id = ? AND f.folw_deleted = 0) 
       OR (u.post_passed_on_by = f.folw_follower_user_id OR u.post_passed_on_by = ? AND u.post_user_id != ? AND u.post_type =4)) 
      ORDER BY 
       post_posted_date DESC 
      LIMIT ? 
     "; 

     $query = $this->db->query($sql, array($uid, $updated, $now, $updated, $now, $uid, $uid, $uid, $limit)); 

     return $query->result_array(); 

    } 

} 

我想拉的結果我home控制器,並與他們合作,我在home控制器寫了這

function index() { 
    if($this->session->userdata('logged_in')) { 

    $session_data = $this->session->userdata('logged_in'); 
    $data['username'] = $session_data['username']; 
    $data['first_name'] = $session_data['first_name']; 
    $data['last_name'] = $session_data['last_name']; 
    $this->load->library('gravatar'); 
    $data['gravatar'] = $this->gravatar->get_gravatar($session_data['username']); 

    //Stream Data 
    $updated = 0.0; 
    $limit = 50; 
    $uid = $session_data['id']; 

    $this->load->model('post'); 
    $data['results'] = $this->post->stream($uid, $updated, $limit); 

    $this->load->view('home_view', $data); 
    } else { 
    //If no session, redirect to login page 
    redirect('login', 'refresh'); 
    } 
} 

但是,當我在我看來echo json_encode($results);我剛剛得到[]返回,請有誰指出我在正確的方向。

+0

我認爲你應該加載模型發佈而不是發佈。與你打電話時一樣。 – Bryan

+0

你在哪裏得到了'db-> query()'語法?我從來沒有見過它。 – Phil

+0

@Phil我發現它與我的CI安裝附帶的文檔是錯誤的嗎? http://ellislab.com/codeigniter/user-guide/database/queries.html –

回答

0

採取@Phil的建議我用$this->db->last_query();,發現查詢所需的變量之一是不正確的。

作爲Codeigniter的新手,我沒有看到所有的調試功能。謝謝stack幫助我理解和解決我的問題。