2014-11-20 136 views
-2

我正在製作一個類似於facebook的網站。爲此,我想實施「長輪詢」以動態更新用戶牆上的新帖子。CakePHP:如何根據創建時間從數據庫獲取數據

這裏是我所計劃

//when user first open my website 
loadPostsNormally(); 
var lastTime = current_time; 
function update(){ 
$.getJSON('/some_other_page_which_will_fetch_the_contents_created_after_lastTime_and_encode_it_to_json/'+lastTime,function(){ 
lastTime = current_time; 
showNewPostsOnWall(); 
} 

setInterval(update,2000); 

現在的問題是我的頁面僞碼some_other_page_which_will_fetch_the_contents_created_after_lastTime_and_encode_it_to_json

public function some_other_page_which_will_fetch_the_contents_created_after_lastTime_and_encode_it_to_json($timestamp){ 
    //fetch_all_data_created_after_this_timestamp_but_how ?? 
} 

我想從我的Post模型來獲取,並在時間我的「創建'的職位表字段就像2014-11-19 22:34:09

對不起,使用非常通俗的語言來描述問題。我之所以這樣做,是因爲我還沒有開始,我只是在制定計劃來解決這個問題。

+0

我在您的文章中看不到問題... – thatidiotguy 2014-11-20 18:07:20

+0

我想知道如何獲取在CakePHP中給定時間後創建的所有數據 – user3774654 2014-11-20 18:09:11

回答

0

Ø只是獲取數據,這很簡單隻需使用條件:

$results = $this->Model->find('all',array(
          'conditions'=>array('Model.date_creation >'=> '2014-11-20'), 
          'recursive'=>1) 
        ); 
+0

這將獲取在2014-11-20創建的數據或所有數據在2014-11-20之後創建? – user3774654 2014-11-20 18:19:49

+0

我使用「>」後的所有數據,因爲在2014-11-20創建的數據不使用任何符號,因爲與該日期不同,使用「<>」。 – 2014-11-20 18:21:58

+0

哦,謝謝。收集! – user3774654 2014-11-20 18:24:37

0

如何獲取基於時間間隔的數據?

所以你有$開始和$結束。

$start = '1800-01-01 00:00:00' //Januuary 1, 1800 or whatever you choose 
$end = date('Y-m-d H:i:s'); // Now 

SELECT * 
FROM `Posts` p 
WHERE (
p.data_created 
BETWEEN $start 
AND $end 
) 

甜!現在你可以整天獲取。