我正在开发一个Symfony2项目,我需要为需要传递一些值的日历使用插件,如下所示:
JS
$.ajax({
type: 'POST',
url: Routing.generate('example'),
data: {},
dataType: 'json',
success: function(response) {
$('#calendar').eCalendar({
...
events: [{
title: 'Example Title 1',
description: 'Description 1.',
datetime: new Date(2016, 11, 30)
},
{
title: 'Example Title 2',
description: 'Description 2.',
datetime: new Date(2016, 10, 23),
}]
});
}
})
这些事件数据可以通过ajax传递如下:
JS
$.ajax({
type: 'POST',
url: Routing.generate('example'),
data: {},
dataType: 'json',
success: function(response) {
for (var i = 0; i < response.data.length; i = i + 1) {
str=response.data[i].datetime.date;
year=str.substring(0,4);
month=str.substring(5,7);
day=str.substring(8,10);
var myDate = new Date(year,month-1,day);
response.data[i].datetime = myDate;
}
$('#calendar').eCalendar({
...
events: response.data
});
}
})
PHP
public function exampleAction()
{
$arr = array(array("title" => 'Example Title 1',"description" => "Description 1.","datetime"=>new \DateTime("now")),
array("title" => 'Example Title 2',"description" => "Description 1.","datetime"=>new \DateTime("now"))
);
return new JsonResponse(array(
'data' =>$arr,
'success' => true), 200);
}
到目前为止一切顺利。但现在我需要从数据库中的下表中获取数据:
id title description datetime category hour
1 Example Title 1 Description 1. 2016-12-21 00:00:00 general all day
2 Example Title 2 Description 2. 2016-12-21 00:00:00 general 09:00
在我的Repository类中,我使用以下代码进行查询:
public function findEvents($category) //$category has the searched value passed from the controller.
{
return $this->getEntityManager()->createQuery(
'SELECT e FROM BackendBundle:Events e WHERE e.category=:category ORDER BY e.datetime ASC, e.hour ASC')
->setParameters(array(
'category' => $category))
->getResult();
}
但现在我不知道如何只使用某些列的值来获取数组关联,如下例所示:
$arr = array(array("title" => 'Example Title 1',"description" => "Description 1.","datetime"=>new \DateTime("now")),
array("title" => 'Example Title 2',"description" => "Description 1.","datetime"=>new \DateTime("now"))
);
答案 0 :(得分:0)
最后感谢@Garry我能用以下代码解决我的问题:
$ cd /var/lib
$ sudo rm -rf ./mongodb
$ sudo mkdir mongodb
$ sudo chown -R mongodb.mongodb mongodb/
$ sudo service mongod restart
除了只获取我需要的三列的值之外,我还可以在标题中添加日期(我之前未在问题中指出)。