2014-11-20 45 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 
) 

甜!现在你可以整天获取。

相关问题