字段在编辑后变为NULL

时间:2014-06-09 04:08:22

标签: php mysql zend-framework2 tablegateway

我开发了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 dosn已定义表单字段。因此,保存时不应该为NULL。

如何解决?

2 个答案:

答案 0 :(得分:0)

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

CREATE TABLE `table` (
     `type` tinyint(3) unsigned NOT NULL default '0',
     .......................

答案 1 :(得分:0)

如果您正在编辑现有记录,则需要首先加载该实体的所有数据,然后然后更新已更改的字段。在ZF2中,这是通过成型水合器实现的;当你绑定'填充'反对表格。

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

<强> EmpolyeeController.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();
    } 
}

您还需要注册一个表格工厂注入保湿剂(其他依赖性)。

<强> Module.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;
            }
        ),
    )
}