类似于教程的编辑操作不起作用

时间:2017-06-28 16:25:48

标签: zend-framework zend-form zend-view zend-framework3

我遵循了ZF3教程并且我非常满意,但是我的控制器中的编辑操作缺少了。添加操作正常,而编辑操作则没有。可能我自己也看不到,所以我在这里发帖,寻求帮助,即使它是非常基本的。

我的模型与教程中的模型几乎相同,这里是其中的一部分:

public function exchangeArray(array $data)
{
    $this->ProjectID= !empty($data['ProjectID']) ? $data['ProjectID'] : null;
    $this->CI_Number= !empty($data['CI_Number']) ? $data['CI_Number'] : null;
    $this->Description= !empty($data['Description']) ? $data['Description'] : null;
    $this->Projectname= !empty($data['Projectname']) ? $data['Projectname'] : null;
    $this->Shortcut= !empty($data['Shortcut']) ? $data['Shortcut'] : null;
    $this->Component_Class= !empty($data['Component_Class']) ? $data['Component_Class'] : null;
}

public function getArrayCopy()
{
//      echo var_dump(get_object_vars($this)
//              );

        //return get_object_vars($this);
        return [
                'ProjectID'     => $this->ProjectID,
                'CI_Number' => $this->CI_Number,
                'Description'  => $this->Description,
                'Projectname'     => $this->Projectname,
                'Shortcut' => $this->Shortcut,
                'Component_Class'  => $this->Component_Class,
        ];
    }

还有我的控制权:

    public function editAction()
{
    $id = (int) $this->params()->fromRoute('id', 0);
    echo $id;
    if (0 === $id) {
        return $this->redirect()->toRoute('project', ['action' => 'add']);
    }
    else {
        try {
            $project = $this->projectTable->getProject($id);
        } catch (\Exception $e) {
            return $this->redirect()->toRoute('project', ['action' => 'index']);
        }
        $form = new ProjectForm();
        $form->bind($project);
        //$form->bind($project->current());
        $form->get('submit')->setAttribute('value', 'save changes');

        $request = $this->getRequest();
        $viewData = ['ProjectID' => $id, 'form' => $form];
        if (! $request->isPost()) {
            return $viewData;
        }
        $form->setInputFilter($project->getInputFilter());
        $form->setData($request->getPost());

        if (! $form->isValid()) {
            echo "nicht valide";
            return $viewData;
        }
        else{
        echo $project;
        $this->projectTable->saveProject($project);
        }
    }
    // Redirect to album list
//  return $this->redirect()->toRoute('project', ['action' => 'index']);
}

这里是完成原因我的观点edit.phtml:

    <?php
$title = 'projects';
$this->headTitle($title);
?>
<h1><?= $this->escapeHtml($title) ?></h1>
<?php

// This provides a default CSS class and placeholder text for the artist element:



$ProjectID= $form->get('ProjectID');
$ProjectID->setAttribute('class', 'form-control');
$ProjectID->setAttribute('placeholder', 'ProjectID');

$Projectname= $form->get('Projectname');
$Projectname->setAttribute('class', 'form-control');
$Projectname->setAttribute('placeholder', 'Projectname');

$CI_Number= $form->get('CI_Number');
$CI_Number->setAttribute('class', 'form-control');
$CI_Number->setAttribute('placeholder', 'CI_number');

$Shortcut= $form->get('Shortcut');
$Shortcut->setAttribute('class', 'form-control');
$Shortcut->setAttribute('placeholder', 'Shortcut');

$Description= $form->get('Description');
$Description->setAttribute('class', 'form-control');
$Description->setAttribute('placeholder', 'Description');

$Component_Class= $form->get('Component_Class');
$Component_Class->setAttribute('class', 'form-control');
$Component_Class->setAttribute('placeholder', 'Component_Class');
// This provides CSS classes for the submit button:
$submit = $form->get('submit');
$submit->setAttribute('class', 'btn btn-primary');

//$form->setAttribute('action', $this->url('project', ['action' => 'edit',]));  //,'id'=> $id
$form->setAttribute('action', $this->url('project', [
    'action' => 'edit',
    'ProjectID'     => $id,
]));
$form->prepare();

echo $this->form()->openTag($form);
?>
<?php // Wrap the elements in divs marked as form groups, and render the
      // label, element, and errors separately within ?>


<div class="form-group">

    <?= $this->formElement($ProjectID) ?>
    <?= $this->formElementErrors()->render($ProjectID, ['class' => 'help-block']) ?>
</div>

<div class="form-group">
    <?= $this->formLabel($CI_Number) ?>
    <?= $this->formElement($CI_Number) ?>
    <?= $this->formElementErrors()->render($CI_Number, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
    <?= $this->formLabel($Description) ?>
    <?= $this->formElement($Description) ?>
    <?= $this->formElementErrors()->render($Description, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
    <?= $this->formLabel($Projectname) ?>
    <?= $this->formElement($Projectname) ?>
    <?= $this->formElementErrors()->render($Projectname, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
    <?= $this->formLabel($Shortcut) ?>
    <?= $this->formElement($Shortcut) ?>
    <?= $this->formElementErrors()->render($Shortcut, ['class' => 'help-block']) ?>
</div>
<div class="form-group">
    <?= $this->formLabel($Component_Class) ?>
    <?= $this->formElement($Component_Class) ?>
    <?= $this->formElementErrors()->render($Component_Class, ['class' => 'help-block']) ?>
</div>


<?php
echo $this->formSubmit($submit);
//echo $this->form->get('DCLID');
echo $this->formHidden($form->get('ProjectID'));
echo $this->form()->closeTag();

它会正确显示记录集,但不会将更改保存到数据库中,然后重定向到我的添加操作。我希望有人看到我所缺少的东西,即使这可能是一个愚蠢的错误。

EDIT1:这是我在index.phtml

中的路由链接
  <a href="<?= $this->url('project', ['action' => 'edit', 'id' => $project->ProjectID]) ?>">Edit</a>

EDIT2:屏幕截图显示路由参数

routing parameter

EDIT3:描述测试问题

这里再次进行我的控制器编辑操作,以跟进:

public function editAction()
{
    $id = (int) $this->params()->fromRoute('id', 0);
    echo "variable id: ". $id;
    if (0 === $id) {
        //return $this->redirect()->toRoute('project', ['action' => 'index']);
        echo "id = 0";
    }
    else {
        try {
            $project = $this->projectTable->getProject($id);
        } catch (\Exception $e) {
            return $this->redirect()->toRoute('project', ['action' => 'index']);
        }
        $form = new ProjectForm();
        $form->bind($project);
        //$form->bind($project->current());
        $form->get('submit')->setAttribute('value', 'save changes');
        var_dump(get_object_vars($project));
        $request = $this->getRequest();
        $viewData = ['ProjectID' => $id, 'form' => $form];
        if (!$request->isPost()) {
            return $viewData;
        }
        else {

            $form->setInputFilter($project->getInputFilter());
            $form->setData($request->getPost());

            if (!$form->isValid()) {
                echo "nicht valide";
                return $viewData;
            }
            else{
                echo "valide";
                echo $project;
                $this->projectTable->saveProject($project);
            }
        }
    }
    // Redirect to album list
//  return $this->redirect()->toRoute('project', ['action' => 'index']);
}

我得到了vardump的输出,因此要编辑的项目将正确传递。如果我发送带有记录更改的表单,控制器将无法捕获它,则控制器操作会看到id = 0,因此将永远不会调用saveProject方法。根据我的理解,它可能与formvalid有关,因为在发送数据之后我来的时间永远不会超过if (0 === $id)

编辑4:经过一些实际测试后,我认为它必定是一个路由问题。如果我通过浏览器手动提供路线,我将无法获得目标页面。如果我转储该值,它始终为NULL。因此,我认为这可能是我module.config.php中显示/更少问题的一个重要因素我在这里展示它因为我无法解决这个问题,我多次将胸罩计算在内并且我不会得到它。所以任何帮助都值得赞赏,它必须是一个非常简单的主题:

    <?php
namespace Import;

use Zend\Router\Http\Segment;
use Zend\Router\Http\Literal;
use Zend\ServiceManager\Factory\InvokableFactory;

//use Zend\ServiceManager\Factory\InvokableFactory;

return [
        /* 'controllers' => [
                'factories' => [
                        Controller\ImportController::class => InvokableFactory::class,
                ],
        ], */


        // hier die Einstellungen für die Routen
        'router' => [
                'routes' => [
                        'index' => [
                                'type'    => Segment::class,
                                'options' => [
                                        'route' => '/import[/:action[/:id]]',
                                        'constraints' => [
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'id'     => '[0-9]+',
                                                        ],
                                        'defaults' => [
                                                'controller' => Controller\IndexController::class,
                                                'action'     => 'index',
                                                        ],
                                            ],
                                    ],

                        'import' => [
                                'type'    => Segment::class,
                                'options' => [
                                        'route' => '/import[/:action[/:id]]',
                                        'constraints' => [
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'id'     => '[0-9]+',
                                        ],
                                        'defaults' => [
                                                'controller' => Controller\ImportController::class,
                                                'action'     => 'index',
                                        ],
                                ],
                        ],

                        'importdcl' => [
                                'type'    => Segment::class,
                                'options' => [
                                        'route' => '/importdcl[/:action[/:id]]',
                                        'constraints' => [
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'id'     => '[0-9]+',
                                        ],
                                        'defaults' => [
                                                'controller' => Controller\ImportdclController::class,
                                                'action'     => 'index',
                                        ],
                                ],
                        ],

                        'project' => [
                                'type'    => Segment::class,
                                'options' => [
                                        'route' => '/project[/:action[/:id]]',
                                        'constraints' => [
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'id'     => '[0-9]+',
                                        ],
                                        'defaults' => [
                                                'controller' => Controller\ProjectController::class,
                                                'action'     => 'index',
                                        ],
                                ],
                        ],
                        'unit' => [
                                'type'    => Segment::class,
                                'options' => [
                                        'route' => '/unit[/:action[/:id]]',
                                        'constraints' => [
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'id'     => '[0-9]+',
                                        ],
                                        'defaults' => [
                                                'controller' => Controller\UnitController::class,
                                                'action'     => 'index',
                                        ],
                                ],
                        ],

                        'index' => [
                                'type'    => Segment::class,
                                'options' => [
                                        'route' => '/index[/:action[/:id]]',
                                        'constraints' => [
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'id'     => '[0-9]+',
                                        ],
                                        'defaults' => [
                                                'controller' => Controller\IndexController::class,
                                                'action'     => 'index',
                                        ],
                                ],
                        ],
                        'user' => [
                                'type'    => Segment::class,
                                'options' => [
                                        'route' => '/user[/:action[/:id]]',
                                        'constraints' => [
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'id'     => '[0-9]+',
                                        ],
                                        'defaults' => [
                                                'controller' => Controller\UserController::class,
                                                'action'     => 'index',
                                        ],
                                ],
                        ],
                        'followup' => [
                                'type'    => Segment::class,
                                'options' => [
                                        'route' => '/followup[/:action[/:id]]',
                                        'constraints' => [
                                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                'id'     => '[0-9]+',
                                        ],
                                        'defaults' => [
                                                'controller' => Controller\FollowupController::class,
                                                'action'     => 'index',
                                        ],
                                ],
                        ],
                ],
        ],



        'view_manager' => [
                'template_path_stack' => [
                        'import' => __DIR__ . '/../view',
                ],
        ],



            /* ... */

        'navigation' => [
                        'default' => [
                                [
                                        'label' => 'Dashboard',
                                        'route' => 'home',

                                ],
                                [
                                        'label' => 'Project',
                                        'route' => 'project',
                                        'pages' => [
                                                [
                                                        'label'  => 'Add',
                                                        'route'  => 'project',
                                                        'action' => 'add',
                                                ],
                                                [
                                                        'label'  => 'Edit',
                                                        'route'  => 'project',
                                                        'action' => 'edit',
                                                ],
                                                [
                                                        'label'  => 'Delete',
                                                        'route'  => 'project',
                                                        'action' => 'delete',
                                                ],
                                        ],
                                ],
                                [
                                        'label' => 'Unit',
                                        'route' => 'unit',
                                        'pages' => [
                                                [
                                                        'label'  => 'Add',
                                                        'route'  => 'unit',
                                                        'action' => 'add',
                                                ],
                                                [
                                                        'label'  => 'Edit',
                                                        'route'  => 'unit',
                                                        'action' => 'edit',
                                                ],
                                                [
                                                        'label'  => 'Delete',
                                                        'route'  => 'unit',
                                                        'action' => 'delete',
                                                ],
                                        ],
                                ],
                                [
                                        'label' => 'Importlog',
                                        'route' => 'importdcl',
                                        'action' => 'index',
                                        'pages' => [
                                                [
                                                        'label'  => 'Add',
                                                        'route'  => 'unit',
                                                        'action' => 'add',
                                                ],
                                                [
                                                        'label'  => 'Edit',
                                                        'route'  => 'unit',
                                                        'action' => 'edit',
                                                ],
                                                [
                                                        'label'  => 'Delete',
                                                        'route'  => 'unit',
                                                        'action' => 'delete',
                                                ],
                                        ],
                                ],

                                [
                                        'label' => 'Follow up',
                                        'route' => 'followup',
                                        'action' => 'index',
                                        'pages' => [
                                                [
                                                        'label'  => 'Add',
                                                        'route'  => 'unit',
                                                        'action' => 'add',
                                                ],
                                                [
                                                        'label'  => 'Edit',
                                                        'route'  => 'unit',
                                                        'action' => 'edit',
                                                ],
                                                [
                                                        'label'  => 'Delete',
                                                        'route'  => 'unit',
                                                        'action' => 'delete',
                                                ],
                                        ],
                                ],

                                [
                                        'label' => 'User',
                                        'route' => 'user',
                                        'pages' => [
                                                [
                                                        'label'  => 'Add',
                                                        'route'  => 'unit',
                                                        'action' => 'add',
                                                ],
                                                [
                                                        'label'  => 'Edit',
                                                        'route'  => 'unit',
                                                        'action' => 'edit',
                                                ],
                                                [
                                                        'label'  => 'Delete',
                                                        'route'  => 'unit',
                                                        'action' => 'delete',
                                                ],
                                        ],
                                ],

                                [
                                        'label' => 'Logout',
                                        'route' => 'user',
                                        'action' => 'logout',
                                        'pages' => [
                                                [
                                                        'label'  => 'Add',
                                                        'route'  => 'unit',
                                                        'action' => 'add',
                                                ],
                                                [
                                                        'label'  => 'Edit',
                                                        'route'  => 'unit',
                                                        'action' => 'edit',
                                                ],
                                                [
                                                        'label'  => 'Delete',
                                                        'route'  => 'unit',
                                                        'action' => 'delete',
                                                ],
                                        ],
                                ],

                        ],
                ],



        /* ... */

];

我目前正在usercontroller进行测试,但我在其他路由中遇到了同样的问题,所以我认为错误可能在此文件中。

2 个答案:

答案 0 :(得分:0)

根据您的代码,似乎问题出在$id上。这意味着值为0,这就是重定向到project/add的原因。

$id = (int) $this->params()->fromRoute('id', 0);
echo $id;
if (0 === $id) {
    return $this->redirect()->toRoute('project', ['action' => 'add']);
}

在您的视图脚本中,表单操作设置为project/edit/ProjectID/$id

$form->setAttribute('action', $this->url('project', [
    'action' => 'edit',
    'ProjectID'     => $id,
]));

这就是您的控制器无法检索$this->params()->fromRoute('id', 0)的原因,因为表格会发送ProjectID参数而不是id

因此,解决方案请调整您的表单(使用id)或您的控制器(使用ProjectID)。

答案 1 :(得分:0)

我终于解决了它。我的建议是对的,这是module.config.php的问题,我在错误的地方设置了一个endbracket。

在将其写入新的部分,完成并完成文件后,它现在可以正常工作。