Pregunta

Since we resolved my last problem (special thanks to ChandlerTi) I've been trying to update my code, to edit and safely delete database records using ZF2 and Doctrine. Now I've stuck on the edit function related problem. When i click on the edit button form opens and is correctly hydrated with record data. Since then, everything is okay. But on save button i receive an error:

C:\Apache24\htdocs\Helpdesk\vendor\doctrine\dbal\lib\Doctrine\DBAL\DBALException.php:47

An exception occurred while executing 'INSERT INTO tbl_incidents (creation_timestamp, engineer, reporter, description, urgency_level_id, status_id) VALUES (?, ?, ?, ?, ?, ?)' with params [null, null, null, null, null, null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null

Ofcourse, thats correct - description should not be null. Bu it is'nt! Ive made a trap to catch POST data sent to sript. They looks to be correct:

POST http://helpdesk.local/incident/add HTTP/1.1 Host: helpdesk.local User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: pl,en-us;q=0.7,en;q=0.3 Accept-Encoding: gzip, deflate Referer: http://helpdesk.local/incident/edit/7 Connection: keep-alive Content-Type: application/x-www-form-urlencoded Content-Length: 75 id=7&reporter=ttt&engineer=ttt&description=ttt&status=1&urgency=1&submit=Go 

All the required fields are filled. Description to. Where is the problem? The only difference between this POST data and the POST generated by the add script (that works correctly) is that add scipt uses PHPSESSID var. And one more thing: my edit code is the same that add code:

IncidentController.php

     public function editAction()
     {
            $incident = new Incident();
            if ($this->params('id') > 0) {
        $incident = $this->getEntityManager()->getRepository('Helpdesk\Entity\Incident')->find($this->params('id'));
    }

    $form = new IncidentForm($this->getEntityManager());
    $form->setHydrator(new DoctrineObject($this->getEntityManager(),'Helpdesk\Entity\Incident'));
    $form->bind($incident);

    $request = $this->getRequest();
    echo $request;
    if ($request->isPost()) {
        $form->setInputFilter($incident->getInputFilter());
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $em = $this->getEntityManager();
            $em->persist($incident);
            $em->flush();

            $this->flashMessenger()->addSuccessMessage('Incident saved');

            // Redirect to list of incidents
            //return $this->redirect()->toRoute('incident');
        }
    }

    $viewModel = new ViewModel(array(
            'incident' => $incident,
            'form' => $form,
    ));
    $viewModel->setTemplate('helpdesk/incident/add.phtml');

    return $viewModel;
}


/**
 * Add action
 * 
 */
public function addAction()
{
    return $this->editAction();
}

I completly dont know what is the difference here between add and edit. A why edit action uses SQL INSERT statement instead of UPDATE. Thanks for any help Smok.

¿Fue útil?

Solución

There are multiple things that you are missing or i think you are doing wrong here, i am saying this as i have no info on what your previous problem was.

1) you are creating a new object $incident = new Incident(); in EDIT where as you need to do something like this, new object will always create empty object with no values

  if (!$this->params('id')) {
     $incident = new Incident(); // means you are coming from add action(which i think is not a good approach the way you are handling it)
}else{
 $incident = $this->getEntityManager()->getRepository('Helpdesk\Entity\Incident')->find($this->params('id'));}     

You will only need

2) I don’t know why you are passing

 $form = new IncidentForm($this->getEntityManager());

Entity manager object to form? if you have specific reason please update your question else i don’t think it is required here. Coz seemingly you are making things tough for your self.

3) Try removing following line

  $form->setHydrator(new DoctrineObject($this->getEntityManager(),'Helpdesk\Entity\Incident'));

4) try replacing this line only if add and edit Template are not identical

 $viewModel->setTemplate('helpdesk/incident/add.phtml');// with
 $viewModel->setTemplate('helpdesk/incident/edit.phtml');// if you dont have edit.phtml create it too

why as i assume your add action would have set its Form action and you will always render a Request similar to ADD.

5) This is my personal opinion that doing some thing like this is bad you need to create proper clean code for every action, for simpler scenarios you may find a work around for little bit tough forms you will stuck and you will have to rewrite whole code

/**
 * Add action
 * 
 */
 public function addAction()
  {
 return $this->editAction();
  }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top