使用 symfony1.4/Doctrine 使用 jquery ajax 调用将 ENUM 字段保存到 MySQL 时出现问题
我目前正在进行一个接收申请的项目,并且我当前正在进行的部分有一个评分者为申请分配手动评分。可能的分数是存储在 MySQL 字段中的枚举值。无论出于何种原因,我似乎无法将值实际保存到数据库中。
我已经尝试过 Doctrines Rawsql 并且尝试了下面的方法(我希望它能工作)。我已经完成了测试,以确保服务器端接收到的值与 SQL 枚举字段相匹配。我尝试包含尽可能多的与该问题相关的代码,但如果需要更多代码,请告诉我!
更新数据库中字段的功能。
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;
}
}
路线:
ajaxSetLongAnswerGrade:
url: /setLongAnswerGrade/:applicationId
class: sfDoctrineRoute
options: { model: Application, type: object}
param: { module: application, action: SetLongAnswerGrade }
requirements:
id: \d+
sf_method: [get]
Ajax 调用:
$(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);
}
});
});});
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!
Function 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);
}
});
});});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在操作中使用
return
时,它会发送响应...您的每个条件都有return
但您没有调用$application->save( );
首先。消除返回,或在每次返回之前调用保存:
我也可能会将此逻辑修改为:
When you use
return
in ana action it sends the response... you havereturn
in each of your conditions but you arent calling$application->save();
first.Elimintate the returns, or call save before each return:
I Would also probably revise this logic to:
这最终出现在我的routing.yml 文件中。
options: { model: Application, type: object}
中缺少method: find
是导致错误的原因。正在进行保存,但不在我想要的记录上(我认为这是隐式的,但在我添加此内容后它立即开始工作)。最终代码...以及ajax...
我非常感谢您的指导!我是在 Java 环境下长大的,但仍在习惯 symfony/php。
This ended up being in my routing.yml file. The lack of
method: find
inoptions: { 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...and the ajax....
I do appreciate the guidance! I was raised Java and still getting used to symfony/php.