簡體   English   中英

Symfony / Doctrine:從processForm獲取最后的插入ID?

[英]Symfony/Doctrine: Getting last insert id from processForm?

在這樣的流程表單請求之后,如何獲取最后的插入ID:

$this->form = new StudyPlanForm();

$this->processForm($request, $this->form);

我會使用save(),但是我想不出一種不必對每個字段都執行$ studyplan-> setField($ request-> getParameter(...))的保存方法。

嘗試:

.. form processing ..
$id = $this->form->getObject()->id;

更新:

僅當它是sfFormObject的實例時,才可以從表單中獲取對象。

像這樣更改您的processForm函數:

protected function processForm(sfWebRequest $request, sfForm $form)
{
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $obj = $form->save();
    }

    return $obj;
}

save方法返回保留在db上的對象,因此您可以從中獲取ID。 因此,您可以執行以下操作:

$this->form = new StudyPlanForm();

$obj = $this->processForm($request, $this->form);
if ($obj != null){
    //do whatever you want like $obj->getId()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM