【问题标题】:How to set a value on a table field with symfony 1.4如何使用 symfony 1.4 在表字段上设置值
【发布时间】:2014-10-13 01:29:26
【问题描述】:

我正在使用 ajax 传递一个值:

$.ajax({
   type: "POST",
   url:  "http://juego.local/frontend_dev.php/espacio/salvaPuntos",
   data: { puntos: 123 }
});

我需要用它更新一个表字段。这就是我正在做的事情(“symfony 方面”):

public function executeSalvaPuntos(sfWebRequest $request) 
{

  $this->puntosEspacio  = $request->getParameter('puntos');
  $this->jugador        = Doctrine::getTable('Jugador')->findOneByEmail($this->getUser()->getUsername());
  $this->puntos         = $this->jugador->getPuntosJuegos() + $this->puntosEspacio;

  $this->form   = new JugadorForm($this->jugador);
  $this->form->setDefault('puntos_juegos', $this->puntos);
  $this->processForm($request, $this->form);
} 

这也是我的 JugadorForm:

public function configure()
{

  unset($this['email'],
        $this['nombre'],
        $this['apellido'],
        $this['puntaje_total'],
        $this['fecha_ult_log'],
        $this['foto_perfil_url']
  );

  $this->setWidgets(array(
    'puntos_juegos'      => new sfWidgetFormInputHidden()
  ));

  $this->widgetSchema->setNameFormat('Jugador[%s]');

  $this->setValidators(array(
    'puntos_juegos'      => new sfValidatorInteger()
  ));   
}

谢谢。

【问题讨论】:

    标签: php ajax database forms symfony-1.4


    【解决方案1】:
    $this->jugador = Doctrine::getTable('Jugador')->findOneByEmail($this->getUser()->getUsername());
    $this->jugador->puntos_juegos += $request->getParameter('puntos');
    $this->jugador->puntos_juegos->save();
    

    【讨论】:

    • 在最后一行你有不必要的puntos_juegos,它应该是$this->jugador->save()。无论如何,我给+1,因为这是一个很好的解决方案。 @gabrielM 如果您直接编辑对象,则无需使用表单。当您让网络用户编辑您的对象时,您应该使用表单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2021-07-15
    相关资源
    最近更新 更多