开发者

Symfony/Doctrine: Getting last insert id from processForm?

How do I get the last insert ID after a process form request such as this one:

$this->form = new StudyPlanForm();

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

I would u开发者_如何学Pythonse save() but I can't figure out a way to save without having to do $studyplan->setField($request->getParameter(...)) for every single field.


Change your processForm function like this:

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

The save method returns the object persisted on db, so you can get the id from that. So you can do:

$this->form = new StudyPlanForm();

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


Try:

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

Update:

It is possible to fetch object from your form only if it is an instance of sfFormObject.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜