0

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.

4

1 回答 1

1

您遗漏了很多东西,或者我认为您在这里做错了,我这么说是因为我不知道您之前的问题是什么。

1)您正在创建一个新对象$incident = new Incident(); EDIT 中,当您需要执行此类操作时,新对象将始终创建没有值的空对象

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

你只需要

2)我不知道你为什么要通过

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

要形成的实体管理器对象?如果您有特定原因,请更新您的问题,否则我认为这里不需要。因为看起来你让自己变得艰难。

3)尝试删除以下行

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

4) 仅当添加和编辑模板不相同时才尝试替换此行

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

为什么我假设您的添加操作会设置其表单操作,并且您将始终呈现类似于添加的请求。

5) 这是我个人的观点,做这样的事情是不好的,你需要为每个动作创建适当的干净代码,对于更简单的场景,你可能会找到一种解决方法来解决你会卡住的有点难的表格,你将不得不重写整个代码

/**
 * Add action
 * 
 */
 public function addAction()
  {
 return $this->editAction();
  }
于 2013-08-11T21:23:09.333 回答