简体   繁体   中英

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 use save() but I can't figure out a way to save without having to do $studyplan->setField($request->getParameter(...)) for every single field.

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 .

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM