2013-05-04 73 views
0

我试图弄清楚为什么我的形式试图插入,而不是更新它的行,但我不知道,这里的代码:

//This function is the one that displays the form 
public function executeIndex(sfWebRequest $request) { 

... 

    $evrescrit = new Criteria(); 
    $evrescrit->addSelectColumn(EvaluacionResumenPeer::STATUS); 
    $evrescrit->addSelectColumn(EvaluacionResumenPeer::RELEVANCE); 
    $evrescrit->addSelectColumn(EvaluacionResumenPeer::QUALITY); 
    $evrescrit->addSelectColumn(EvaluacionResumenPeer::COMMENT); 
    $evrescrit->add(EvaluacionResumenPeer::RESUMEN_ID, $this->resumen->getId()); 

    $this->form = new EvaluacionResumenForm(EvaluacionResumenPeer::retrieveByPK($this->resumen->getId()), array('criteria' => $evrescrit)); 
    //$this->form = new EvaluacionResumenForm(EvaluacionResumenPeer::retrieveByPK($this->resumen->getId())); 
} 


//The form, I'm using the Base but unsetting some columns, PK and FK. 
class EvaluacionResumenForm extends BaseEvaluacionResumenForm { 

public function configure() { 
    unset(
      $this['created_at'], $this['updated_at'], $this['evaluador_id'], $this['resumen_id'] 
    ); 
} 

} 


//This is where the form submits 
public function executeEdit(sfWebRequest $request) { 

    $user = $this->getUser()->getGuardUser(); 
    $this->forward404Unless($user); 
    $this->form = new EvaluacionResumenForm(); 

    if ($request->isMethod(sfRequest::POST)) { 
     $this->processEdit($request, $this->form); 
    } 
} 

public function processEdit(sfWebRequest $request, sfForm $form) { 

    $this->form->bind($request->getParameter($form->getName())); 
    //$this->form->bind($request->getPostParameter($request->getPostParameter('status'))); 

    $errors = $this->form->getErrorSchema()->getErrors(); 
    if (count($errors) > 0) { 
     foreach ($errors as $name => $error) { 
      echo $name . ': ' . $error . '<br>'; 
      echo $request->getParameter('evaluacion_resumen[resumen_id]'); 
     } 
    } 

    if ($this->form->isValid()) { 
     $this->form->save(); 
     $this->getUser()->setFlash('notice', 'Sus modificaciones han sido grabadas.'); 
     $this->redirect('@resumenes_asignados'); 
    } 
} 

在模板我用它来呈现形式:

<?php echo $form->renderHiddenFields(); ?> 
<?php echo $form; ?> 

我得到的错误,当我提交表单(我从数据库中的值,并用这种形式进行更新)是这一个:

Unable to execute INSERT statement. [wrapped: SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (`abstracts/evaluacion_resumen`, CONSTRAINT `evaluacion_resumen_FK_1` FOREIGN KEY (`resumen_id`) REFERENCES `resumen` (`id`))] 

我真的不知道为什么它试图插入一个新的行,我需要更新现有的。

谢谢。

回答

1

这是因为executeEdit()函数中的下面一段代码。从sfFormPropel

$this->form = new EvaluacionResumenForm(); 

BaseEvaluacionResumenForm继承。 sfFormPropel将Propel对象作为其构造函数的第一个参数。如果没有对象通过(就像你的情况一样),它将使用getModelName()函数来确定这个表单在你的模型中使用哪个对象并创建一个新对象(见下文)。

// \lib\vendor\....\sfFormPropel.class.php 
public function __construct(BaseObject $object = null, $options = array(), $CSRFSecret = null) 
    { 
    $class = $this->getModelName(); 
    if (is_null($object)) 
    { 
     $this->object = new $class(); 
    } 

所以当形式保存,它的保存新的对象,这就是为什么你要插入而不是更新的。

你想要做这样的事情:

// This is where the form submits 
// Example url, http://mysite.com/moduleName/edit/id/:id 
public function executeEdit(sfWebRequest $request) { 

    $user = $this->getUser()->getGuardUser(); 
    $this->forward404Unless($user); 

    // Get the primary key of the object we want to use or forward 404 
    $this->forward404Unless($id = $request->getParameter('id'), "Required parameter 'id' must be present in request"); 

    // Retrieve the object from the database using the id or forward 404 
    $this->forward404Unless($er = EvaluacionResumenPeer::retrieveByPK($id), sprintf("No object could be retrieved by %s", $id)); 

    $this->form = new EvaluacionResumenForm($er); 

    if ($request->isMethod(sfRequest::POST)) { 
     $this->processEdit($request, $this->form); 
    } 
} 

作为一个附加的检查,您可以更改请求的类型在您的表单模板把当对象是不是新象下面这样。这将确保您不会在编辑操作中意外插入任何新对象。

// apps\appName\modules\moduleName\templates\editSuccess.php 
if (!$form->isNew()): ?> 
    <input type="hidden" name="sf_method" value="PUT" /> 
<?php endif ?> 

然后在您的executeEdit()函数中。如果请求是POST类型,检查它是否为PUT,则更改该行。

if ($request->isMethod(sfRequest::PUT)) { 
+0

非常感谢!它像一个魅力。 – MauJFernandez 2013-05-05 23:41:54

相关问题