1
を:MySQLのクエリ結果から連想配列の作成 - 私はSymfony2のプロジェクトに取り組んでいると私は次のようにいくつかの値を渡す必要があるカレンダーのプラグインを使用する必要がDoctrine2
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
});
}
})
P HP
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
:しかし、今、私は、データベース内の次のテーブルからそのデータを取得する必要があります
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"))
);
部分的な部分を使用してください。{title、description、etc.} FROM ... ' – yceruto
'$ this-> getEntityManager() - > createQuery(' SELECT e.title、e.description、e .date FROM BackendBundle:イベントe。カテゴリ:カテゴリーORDER BY e.date ASC、e.hour ASC ') - > setParameters(array(' category '=> $ category)) - > getResult(Doctrine \ ORM \クエリ:: HYDRATE_ARRAY); ' – Garry
ありがとう@ギャリー、エラーを与えた' Doctrine \ ORM \ Query :: HYDRATE_ARRAY'を除いて、私があなたに教えてくれるものを追加してこれを得ました(私はそれが何であるかわかりません)。私が以前に言及していない問題は、タイトル列の値の最後に時間列のテキストを追加する必要があるということです(例:Title1。| 09:00)。次に、それを修正し、変更された連想配列を作成します。その情報をどのように追加できますか? – Joseph