I have a simple question.
I am working on Zend framework 2.
I am trying to make an AJAX call to fetch all booking data from table booking
by passing the booking_id. Trouble is, the query is failing for reasons unknown. The actual query is complex and its working when I replace $booking_id with an actual booking_id like '22432'. Hence, I believe that the query is fine, there is some other issue.
But I don't know how to fetch the query errors/exceptions in an Ajax call. Can someone help me with this?
Javascript:
$.post("dashboard/getBookingDataByBookingId", {
booking_id: bookingId,
},
function(data){
if(data.response == true) {
alert(data);
} else {
alert('failed');
}
}, 'json');
Controller
public function getBookingDataByBookingIdAction()
{
$request = $this->getRequest();
$response = $this->getResponse();
if ($request->isPost())
{
$post_data = $request->getPost();
$booking_id = $post_data['booking_id'];
$booking_data = array();
$booking_data = $this->getBookingTable()->getBookingByUserIdAndBookingId($booking_id);
if (!$booking_data)
$response->setContent(\Zend\Json\Json::encode(array('response' => false, 'booking_data' => $booking_data)));
else {
$response->setContent(\Zend\Json\Json::encode(array('response' => true, 'booking_data' => $booking_data)));
}
}
return $response;
}
The bookingTable model has a public function:
public function getBookingByUserIdAndBookingId($booking_id)
{
$sql = "Select * from booking where id='".$booking_id."';
try {
$statement = $this->adapter->query($sql);
$res = $statement->execute();
return $res->current();
} catch (Exception $ex) {
return $ex;
}
}