2

我正在学习如何使用 Zend Framework2。根据网上的一些教程,我写了一些代码。对我来说最重要的教程是这个:https ://github.com/psamatt/zf2-doctrine-example它涵盖了我计划编写的大部分基础知识。我一直坚持一个对我来说很奇怪的问题。在我的摘要页面上,显示来自数据库的所有记录,我有一个添加新记录、编辑现有记录和删除记​​录的链接。路由由 module.config.php 覆盖:

        'router' => array(
            'routes' => array(
                    'incident' => array(
                            'type'    => 'segment',
                            'options' => array(
                                    'route'    => '/incident[/][:action][/:id]',
                                    'constraints' => array(
                                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'id'     => '[0-9]+',
                                    ),
                                    'defaults' => array(
                                            'controller' => 'Helpdesk\Controller\Incident',
                                            'action'     => 'index',
                                    ),
                            ),
                    ),
            ),
    ),

当我使用指向新记录的链接 (http://helpdesk/incident/add) 时,一切正常。但是,当我使用链接编辑我的记录时(http://helpdesk/incident/edit/1 - 其中 1 是示例记录 ID),我收到一个错误:

Zend\View\Renderer\PhpRenderer::render: Unable to render template "helpdesk/incident/edit"; resolver could not resolve to a file

这是我的 IncidentController.php:

<?php
namespace Helpdesk\Controller;

use Application\Controller\EntityUsingController;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
use Doctrine\ORM\EntityManager;
use Zend\View\Model\ViewModel;

use Helpdesk\Form\IncidentForm;
use Helpdesk\Entity\Incident;



class IncidentController extends EntityUsingController
{

/**
 * Index action
 * 
 */
public function indexAction()
{
    $em = $this->getEntityManager();

    $incidents = $em->getRepository('Helpdesk\Entity\Incident')->findAll();

    return new ViewModel(array(
            'incidents' => $incidents 
    ));
}


/**
 * Edit action
 * 
 */
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->bind($incident);
    $form->setHydrator(new DoctrineObject($this->getEntityManager(),'Helpdesk\Entity\Incident'));

    $request = $this->getRequest();

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

    return array(
            'incident' => $incident,
            'form' => $form,
    );
}


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


/**
 * Delete action
 * 
 */
public function deleteAction()
{
    $id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
    if (!$id) {
        return $this->redirect()->toRoute('incident');
    }

    $request = $this->getRequest();
    if ($request->isPost()) {
        $del = $request->post()->get('del', 'No');
        if ($del == 'Yes') {
            $id = (int)$request->post()->get('id');
            $incident = $this->getEntityManager()->find('Helpdesk\Entity\Incident', $id);
            if ($incident) {
                $this->getEntityManager()->remove($incident);
                $this->getEntityManager()->flush();
            }
        }

        // Redirect to list of incidents
        return $this->redirect()->toRoute('default', array(
                'controller' => 'incident',
                'action' => 'index',
        ));
    }

    return array(
            'id' => $id,
            'incident' => $this->getEntityManager()->find('Helpdesk\Entity\Incident', $id)->getArrayCopy()
    );
}
}

这两者有什么区别?为什么一个工作正常,而第二个产生错误?

谢谢你的帮助

抽烟。

4

1 回答 1

1

很可能 helpdesk/incident/edit.phtml 不存在,而添加操作正在呈现现有的 helpdesk/incident/add.phtml。

您可以重复使用现有的 helpdesk/incident/add.phtml 或创建一个新的。

于 2013-07-10T11:41:45.660 回答