2016-12-01 1 views
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")) 
    ); 
+1

部分的な部分を使用してください。{title、description、etc.} FROM ... ' – yceruto

+2

'$ 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

+0

ありがとう@ギャリー、エラーを与えた' Doctrine \ ORM \ Query :: HYDRATE_ARRAY'を除いて、私があなたに教えてくれるものを追加してこれを得ました(私はそれが何であるかわかりません)。私が以前に言及していない問題は、タイトル列の値の最後に時間列のテキストを追加する必要があるということです(例:Title1。| 09:00)。次に、それを修正し、変更された連想配列を作成します。その情報をどのように追加できますか? – Joseph

答えて

0

おかげで、私は次のコードで私の問題を解決することができた@Garryする:

public function findEvents($category) //$category has the searched value passed from the controller. 
{ 
    return $this->getEntityManager()->createQuery(
    'SELECT CONCAT(CONCAT(e.title, ' '), e.date) AS title, e.description, e.datetime FROM BackendBundle:Events e WHERE e.category=:category ORDER BY e.datetime ASC, e.hour ASC') 
    ->setParameters(array(
    'category' => $category)) 
    ->getResult(); 
} 

私が必要とした3つの列の値だけでなく、タイトルに日付を追加することもできました。

関連する問題