-1
UPDATESymfony2中使用Ajax使用,以獲得在教義查詢的結果
我在廣告的庫函數
public function findByExceptOwner($paramFetcher)
{
$query = $this->getEntityManager()->createQueryBuilder('ad');
// parâmetros iniciais da query: status, departure de destination
$query->select('ad');
if(array_key_exists('type', $paramFetcher)){
$query->from('DelivveWebBundle:' . $paramFetcher['type'], 'ad');
}else{
$query->from('DelivveWebBundle:Ad', 'ad');
}
$query
->where('ad.owner <> :ownerId')
->andWhere($query->expr()->eq('ad.status', ':status'))
->andWhere('ad.dateFinal <= :today')
->setParameter('ownerId', $paramFetcher['ownerId'])
->setParameter('status', 'new')
->setParameter('today', new \DateTime("now"));
if (array_key_exists('departure',$paramFetcher)) {
$departureArray = preg_split('/ - /', $paramFetcher['departure']);
$departureCityArray = preg_split('/,/', $departureArray[count($departureArray) - 2]);
$departureUFArray = preg_split('/,/', $departureArray[count($departureArray) - 1]);
$departure = $departureCityArray[count($departureCityArray) - 1] . ' - ' . $departureUFArray[0];
$query
->andWhere($query->expr()->like('ad.departure', ':departure'))
->setParameter('departure', "%{$departure}%");
}
if (array_key_exists('destination',$paramFetcher)) {
$destinationArray = preg_split('/ - /', $paramFetcher['destination']);
$destinationCityArray = preg_split('/,/', $destinationArray[count($destinationArray) - 2]);
$destinationUFArray = preg_split('/,/', $destinationArray[count($destinationArray) - 1]);
$destination = $destinationCityArray[count($destinationCityArray) - 1] . ' - ' . $destinationUFArray[0];
$query
->andWhere($query->expr()->like('ad.destination', ':destination'))
->setParameter('destination', "%{$destination}%");
}
// raio de busca com centro nas coordenadas de departure
if (array_key_exists('departure_latitude', $paramFetcher) && array_key_exists('departure_longitude', $paramFetcher) && array_key_exists('radius', $paramFetcher)) {
$query
->andWhere('earth_distance_operator(earth_box(ll_to_earth(:departure_lat, :departure_lon), :radius), \'@>\', ll_to_earth(ad.departureLatitude, ad.departureLongitude)) = true')
->setParameter('departure_lat', floatval($paramFetcher['departure_latitude']), \Doctrine\DBAL\Types\Type::FLOAT)
->setParameter('departure_lon', floatval($paramFetcher['departure_longitude']), \Doctrine\DBAL\Types\Type::FLOAT)
->setParameter('radius', floatval($paramFetcher['radius'])/1.609, \Doctrine\DBAL\Types\Type::FLOAT);
}
// raio de busca com centro nas coordenadas de destionation
if (array_key_exists('destination_latitude', $paramFetcher) && array_key_exists('destination_longitude', $paramFetcher) && array_key_exists('radius', $paramFetcher)) {
$query
->andWhere('earth_distance_operator(earth_box(ll_to_earth(:destination_lat, :destination_lon), :radius), \'@>\', ll_to_earth(ad.destinationLatitude, ad.destinationLongitude)) = true')
->setParameter('destination_lat', floatval($paramFetcher['destination_latitude']), \Doctrine\DBAL\Types\Type::FLOAT)
->setParameter('destination_lon', floatval($paramFetcher['destination_longitude']), \Doctrine\DBAL\Types\Type::FLOAT)
->setParameter('radius', floatval($paramFetcher['radius'])/1.609, \Doctrine\DBAL\Types\Type::FLOAT);
}
return $query->getQuery()->getResult();
}
我在控制器下面的方法
public function searchAdsAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$em = $this->getDoctrine()->getManager();
$owner = $this->getUser();
$parameter = array();
$parameter["departure_latitude"] = $request->request->get('departure_lat');
$parameter["departure_longitude"] = $request->request->get('departure_long');
$parameter["radius"] = Constant::RADIUS;
$parameter["ownerId"] = $owner->getId();
$entities = $em->getRepository('DelivveWebBundle:Ad')->findByExceptOwner($parameter);
return new JsonResponse($entities);
}
我的路線寫成如下
search_ads:
pattern: /ad/search
defaults: { _controller: DelivveWebBundle:Ad:searchAds }
我的功能AJAX
$(window).load(function() {
var url = window.location.href;
var urlArray = url.split("/");
var path = urlArray[0]+"//"+urlArray[2];
var departureLatitude = $("#bundle_ad_departureLatitude").val();
var departureLongitude = $("#bundle_ad_departureLongitude").val();
$.post(path+"{{ path('search_ads') }}",
{departure_lat: departureLatitude, departure_long: departureLongitude},
function (entities){
alert("success ");
console.log(entities);
$(".panel-foot-information").remove();
$div = "<div class='panel-foot-information row big-not-visible'>";
if (entities != null){
$div = $div + "<table id='ads'><thead><tr><th>Departure</th><th>Destination</th></tr></thead><tbody>";
$.each(entities, function(key, value) {
$div = $div + "<tr class='ad-tr'>";
$div = $div + "<td class='hidden'><input type='hidden' id='idLat' value="+ value.departureLatitude +"/></td>";
$div = $div + "<td class='hidden'><input type='hidden' id='idLong' value="+ value.departureLongitude +"/></td>";
$div = $div + "<td>"+ value.owner.username +"</td>";
$div = $div + "<td>"+ value.departure +"</td>";
$div = $div + "<td>"+ value.destination +"</td>";
$div = $div + "<td class='packageType'>"+ value.packageType +"</td>";
$div = $div + "<td class='transportation'>"+ value.transportation +"</td>";
$div = $div + "<td class='price'>"+ value.price +"</td>";
$div = $div + "<td class='date'>"+ value.date + " - " + value.dateFinal +"</td>";
$div = $div + "</tr>";
alert($div)
});
$div = $div + "</tbody></table></div>";
}
$div = $div + "</div>";
$(".panel-middle").append($div);
}, "json"
);
});
我想對於那些誰看到我anterirormente問題的另一個解決方案。
經過很多人在stackoverflow中搜索和懷疑可以達到它,但是當我回到ajax上時,他有一張對象類型的照片,這沒有任何意義。
任何人都知道如何做到這一點?
直接在瀏覽器中打開頁面,並檢查日誌以查看觸發500錯誤的原因。你可以從你的問題中刪除AJAX代碼,因爲它似乎與500錯誤無關。 –
錯誤500由您的服務器報告,大概由您的PHP代碼報告。在使用帶有AJAX的輸出之前檢查您的控制器是否工作正常。 –
對不起,我不明白你的意見。如果您的AJAX由於500錯誤而無法使用,請在繼續之前進行修復。 –