0

我已经开发了 Zend 2 应用程序。有表格可以编辑现有数据。表格中的某些字段不包含在表单中。因此,在编辑这些记录时,不在表单中的字段将保存为 NULL。如何解决?

模型 -

namespace Employee\Model;

class Employee
{
    public $id;
    public $active;
    public $type;
    public $mailing_address;
    public $permanent_address;

...

public function exchangeArray($data)
{
$this->id     = (isset($data['id'])) ? $data['id'] : 0;
$this->active = (isset($data['active'])) ? $data['active'] : 0;
$this->type  = (isset($data['type'])) ? $data['type'] : null;
$this->mailing_address  = (isset($data['mailing_address'])) ? $data['mailing_address'] : null;
$this->permanent_address  = (isset($data['permanent_address'])) ? $data['permanent_address'] : null;

...

桌子 -

public function saveEmployee(Employee $employee) {

        $data = array(
            'active' => $employee->active,
            'type' => $employee->type,
            'mailing_address' => $employee->mailing_address,
            'permanent_address' => $employee->permanent_address,
...

$id = (int) $employee->id;
    if ($id == 0) {
        $inserted = $this->tableGateway->insert($data);
        $inserted_id = $this->tableGateway->lastInsertValue;
    } else {
        if ($this->getEmployee($id)) {
            $this->tableGateway->update($data, array('id' => $id));
            $inserted_id = $id;
        } else {
            throw new \Exception('Employee does not exist');
        }
    }
    return $inserted_id;
    //\Zend\Debug\Debug::dump($inserted_ids);
}

控制器 -

$employeeForm = new EmployeeForm();
$employeeForm->bind($employee);
$request = $this->getRequest();

if ($request->isPost()) {
    $employeeForm->setData($request->getPost());

    if ($employeeForm->isValid()) {
       $this->getEmployeeTable()->saveEmployee($employee);
    }
}

假设type没有定义表单字段。因此,保存时不应为 NULL。

如何解决?

4

2 回答 2

0

尝试用mysql处理它。明智地使用每个字段的 [default] 功能

CREATE TABLE `table` (
     `type` tinyint(3) unsigned NOT NULL default '0',
     .......................
于 2014-06-09T06:38:56.710 回答
0

如果您正在编辑现有记录,则需要首先加载该实体的所有数据,然后更新已更改的字段。在 ZF2 中,这是通过形式水合器实现的;当您将“填充”对象绑定到表单时。

因此,您的控制器代码需要更改。

EmployeeController.php

// Fetch the form from the service manager
// allowing it to be created via factory and have our
// hydrator and entity class injected
$form = $this->serviceLocator()->get('MyModule\Form\EmployeeForm');
$request = $this->getRequest();
$id = $this->params('id'); // Employee ID as route param

// Load the employee data from the database
// (this will vary dependning your own strategy, however
// a service layer is assumed)
$employee = $this->employeeService->findById($id);

// Bind the **hydrated** entity to the form
$form->bind($employee);

if ($request->isPost()) {

    // set the modified post data
    $form->setData($request->getPost());

    if ($form->isValid()) {

        // Retrive the validated and updated entity
        $employee = $form->getData();
    } 
}

您还需要注册一个表单工厂来注入水合器(其他依赖项)。

模块.php

public function getFormElementConifg()
{
    return array(
        'factories' => array(
            'MyModule\Form\EmployeeForm' => function($formElementManager) {

                $serviceManager = $formElementManager->getServiceLocator();
                $hydrator = $serviceManager->get('MyModule\Stdlib\Hydrator\EmployeeHydrator');

                $form = new Form\EmployeeForm();

                $form->setHydrator($hydrator);
                $form->bind(new Entity\Employee());


                return $form;
            }
        ),
    )
}
于 2014-06-09T08:31:03.533 回答