ZF2表格与模型绑定

时间:2014-04-09 10:15:32

标签: php forms zend-framework2

我正在尝试“Zend Framework 2.0 by Example”一书。我正在编辑个人资料;我们在哪里有

的表格
user_id
user_first_name
User_middle_name
user_last_name
user_email
user_password
user_password_seed
user_status_code
user_role_code

表单包含以下字段

user_id - hidden
user_first_name
User_middle_name
user_last_name
user_email

我有动作控制器代码如下

$this->viewModel = new ViewModel ( );
$request = $this->getRequest ( );
$form = $this->getServiceLocator ( )->get ( 'ModifyUserForm' );
$userTable = $this->getServiceLocator ( )->get ( 'UserTable' );
$user_id = $this->params ( )->fromRoute ( 'id' );
$user = $userTable->getUser ( $user_id );
$before = $userTable->getUser ( $user_id );
$this->viewModel->post = $request->getPost( );
$this->viewModel->beforebinduser = $before;
$form->bind ( $user );
$form->setData ( $request->getPost ( ) );
$this->viewModel->afterbinduser = $user;

#$form->setValidationGroup( 'user_first_name', 'user_middle_name', 'user_last_name', 'user_email' );
if ( $form->isValid ( ) ) {
    $userTable->saveUser ( $user );
    $this->viewModel->aftersaveuser = $user;
}

我遇到的问题是表单中没有的所有列都替换为null; 我一整天都在网上搜索过这个但仍未解决的问题。

模型中saveuser的代码

 public function saveUser ( User $user ) {

        $data = array (
            'user_id' => $user->user_id,
            'user_email' => $user->user_email,
            'user_first_name' => $user->user_first_name,
            'user_middle_name' => $user->user_middle_name,
            'user_last_name' => $user->user_last_name,
            'user_status_code' => $user->user_status_code,
            'user_password' => $user->user_password,
            'user_password_seed' => $user->user_password_seed,
            'user_login_enabled_flag' => $user->user_login_enabled_flag,
            'user_role_code' => $user->user_role_code,
            'create_userid' => $user->create_userid,
            'create_timestamp' => $user->create_timestamp,
            'last_update_userid' => $user->last_update_userid,
            'last_update_timestamp' => $user->last_update_timestamp,
        );

        $user_id = ( int ) $user->user_id;
        #need to set the correct last update userid
        $data [ 'last_update_userid' ] = 1;
        $data [ 'last_update_timestamp' ] = date ( 'Y-m-d H:i:s' );
        if ( $user_id === 0 ) {
            #need to set the correct create userid
            $data [ 'user_status_code' ] = "ACTIVE";
            $data [ 'user_role_code' ] = "ADMIN";
            $data [ 'user_login_enabled_flag' ] = "Y";
            $data [ 'create_userid' ] = 1;
            $data [ 'create_timestamp' ] = date ( 'Y-m-d H:i:s' );
            $this->tableGateway->insert( $data );
        } else {
            if ( $this->getUser ( $user_id ) ) {
                if ( empty( $data[ 'user_password' ] ) ) {
                    unset( $data[ 'user_password' ] );
                }
                $this->tableGateway->update( $data, array ( 'user_id' => $user_id ) );
            } else {
            throw new \Exception ( "Could not find row in table $this->table with $user_id for update" );
            }
        }

    }

提前感谢您的帮助!!!

1 个答案:

答案 0 :(得分:1)

创建NullObject并为您的表单设置Hydrator

Hydrator会将给定数据(从您的请求)保存到您的NullObject

<?php
/* other stuff */
$form->setHydrator(\Zend\Stdlib\Hydrator\ClassMethods); /* <--- this line to set zf2 ClassMethods Hydrator */
$form->bind($user); /* $user should be the NullObject */
$form->setData($request->getPost());
$this->viewModel->afterbinduser = $user;

#$form->setValidationGroup( 'user_first_name', 'user_middle_name', 'user_last_name', 'user_email' );
if ($form->isValid()) {
    $userTable->saveUser($user);
    $this->viewModel->aftersaveuser = $user;
}