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