使用 symfony1.4/Doctrine 使用 jquery ajax 调用将 ENUM 字段保存到 MySQL 时出现问题

发布于 2024-11-02 17:54:45 字数 2600 浏览 0 评论 0原文

我目前正在进行一个接收申请的项目,并且我当前正在进行的部分有一个评分者为申请分配手动评分。可能的分数是存储在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的奇迹 2024-11-09 17:54:45

当您在操作中使用 return 时,它会发送响应...您的每个条件都有 return 但您没有调用 $application->save( ); 首先。

消除返回,或在每次返回之前调用保存:

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;
    }
}

我也可能会将此逻辑修改为:

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;

   }
}

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;

   }
}
绳情 2024-11-09 17:54:45

这最终出现在我的routing.yml 文件中。 options: { model: Application, type: object} 中缺少 method: find 是导致错误的原因。正在进行保存,但不在我想要的记录上(我认为这是隐式的,但在我添加此内容后它立即开始工作)。最终代码...

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;
}

以及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);
                  }
    });

我非常感谢您的指导!我是在 Java 环境下长大的,但仍在习惯 symfony/php。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文