Problem saving ENUM field to MySQL using symfony1.4/Doctrine using jquery ajax call
I'm currently on a project that receives applications and the part I'm currently on has a grader that assigns a manual grade for the application. The possible scores are enum values stored in a MySQL field. For whatever reason I cannot seem to get the value to actually be save to the database.
I have tried Doctrines Rawsql and I have tried the method below (that I would expect to work). I have done testing to ensure that the values received on the server side to match the SQL enum field. I've tried to include as much code as possible that I see as pertaining to the issue, but if there's more needed just let me know!
Functi开发者_如何学运维on to update field in DB.
public function executeSetLongAnswerGrade(sfWebRequest $request){
$application = $this->getRoute()->getObject();
if($request->isXmlHttpRequest()){
if($request->getParameter("methodCall") == "Passion"){
$application->setGradepassion($request->getParameter('value'));
return true;
}elseif($request->getParameter("methodCall") == "Grammer"){
$application->setGradegrammer($request->getParameter('value'));
}elseif($request->getParameter('methodCall') == "Thought"){
$application->setGradethought($request->getParameter('value'));
return true;
}
$application->save();
return true;
}
}
Route:
ajaxSetLongAnswerGrade:
url: /setLongAnswerGrade/:applicationId
class: sfDoctrineRoute
options: { model: Application, type: object}
param: { module: application, action: SetLongAnswerGrade }
requirements:
id: \d+
sf_method: [get]
Ajax Call:
$(document).ready(function(){
$('#passionMSG').hide();
$('#grammerMSG').hide();
$('#thoughtMSG').hide();
$('.passionSuccess').hide();$('.passionError').hide();
$('.grammerSuccess').hide();$('.grammerError').hide();
$('.thoughtSuccess').hide();$('.thoughtError').hide();
$('#passion').change(function()
{
$('#passion').attr('disabled', true);
$('.passionSuccess').hide();
$('#passionMSG').slideDown(200);
$('.passionError').hide();
$.ajax({
url: '<?php echo url_for2('ajaxSetLongAnswerGrade', $application) ?>',
data: { methodCall: "Passion",
value: this.value} ,
success: function(data) {
$('#passion').attr('disabled', false);
$('#passionMSG').slideUp(1500)
$('.passionSuccess').delay(1300).slideDown(2000);
},
error: function(){
$('#passion').attr('disabled', false);
$('#passionMSG').slideUp(1500)
$('.passionError').delay(1300).slideDown(2000);
}
});
});});
When you use return
in ana action it sends the response... you have return
in each of your conditions but you arent calling $application->save();
first.
Elimintate the returns, or call save before each return:
public function executeSetLongAnswerGrade(sfWebRequest $request){
$application = $this->getRoute()->getObject();
if($request->isXmlHttpRequest()){
if($request->getParameter("methodCall") == "Passion"){
$application->setGradepassion($request->getParameter('value'));
}elseif($request->getParameter("methodCall") == "Grammer"){
$application->setGradegrammer($request->getParameter('value'));
}elseif($request->getParameter('methodCall') == "Thought"){
$application->setGradethought($request->getParameter('value'));
}
$application->save();
return true;
}
}
I Would also probably revise this logic to:
public function executeSetLongAnswerGrade(sfWebRequest $request)
{
$application = $this->getRoute()->getObject();
if($request->isXmlHttpRequest())
{
$call = $request->getParameter('methodCall');
$value = $request->getParameter('value');
switch($call)
{
case 'Passion':
$application->setGradepassion($value);
break;
case 'Grammer':
$application->setGradegrammer($value);
break;
case 'Thought':
$application->setGradethought($value);
break;
default:
throw new Exception('Invalid methodCall.');
}
$application->save();
return sfView::NONE;
}
}
This ended up being in my routing.yml file. The lack of method: find
in options: { model: Application, type: object}
is what was causing the error. A save was occuring, but not on the record I was wanting (I thought this was implicit but it instantly started working after I added this). final code...
public function executeSetLongAnswerGrade(sfWebRequest $request){
$application = $this->getRoute()->getObject();
if($request->isXmlHttpRequest()){
$application->{$request->getParameter('value')}($request->getParameter('value'));
$application->save();
return sfView::NONE;
}else{
return false;
}
and the ajax....
$('#passion').attr('disabled', true);
$('.passionSuccess').hide();
$('#passionMSG').slideDown(200);
$('.passionError').hide();
$.ajax({
url: '<?php echo url_for2('ajaxSetLongAnswerGrade', $application) ?>',
data: { methodCall: "setGradepassion",
value: this.value} ,
success: function(data) {
$('#passion').attr('disabled', false);
$('#passionMSG').slideUp(1500)
$('.passionSuccess').delay(1300).slideDown(2000);
},
error: function(){
$('#passion').attr('disabled', false);
$('#passionMSG').slideUp(1500)
$('.passionError').delay(1300).slideDown(2000);
}
});
I do appreciate the guidance! I was raised Java and still getting used to symfony/php.
精彩评论