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.