ZF2使用AbstractTableGateway添加更新限制

时间:2015-01-20 18:15:53

标签: php zend-framework2

我使用ZF2 TableGateway来更新一些数据:

$this->update($data, array('id' => $id)));

我想为此查询添加限制,例如:

$this->update($data, array('id' => $id)))->limit(1);

然而,这并不起作用。任何想法如何实现这一目标?

谢谢!

1 个答案:

答案 0 :(得分:0)

在你的控制器中使用它:

导入:

   use Zend\Db\TableGateway\TableGateway
主控制器中的

    public function updateuserAction() {

    $form = new UserRegistration();
    $request = $this->getRequest();

    $id = $this->params()->fromRoute('id');

    if(!$id) {
        $this->redirect()->toUrl('/REDIRECT_URL');
    }

    if ($request->isPost()) {
        $registeruser = new UserRegistrationModel();
        $formValidator = new UserRegistrationValidator(); {
            $form->setInputFilter($formValidator->getInputFilter());
            $form->setData($request->getPost());
        }

        if ($form->isValid()) {
            $data = $form->getData();
            unset($data['submit']);

            $this->getUsersTable()->update($data, array('uid' => $id));

            return $this->redirect()->toUrl('/REDIRECT_URL');
        }
        $view = new ViewModel(array('form' => $form, 'action' => $this->params()->fromRoute('action')));
        return $view;
    }
    else {
        $form->setData($this->getUsersTable()->select(array('uid' => $id))->current());
    }

    $view = new ViewModel(array('form' => $form, 'id' => $id , 'rowset' => $this->getUsersTable()->select(array('uid' => $id))->current()));
    return $view;
}


public function getUsersTable() {
        if (!$this->RegisterUser) {
            $article = $this->RegisterUser = new TableGateway(
                    'users', $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter')
            );
        }
        return $this->RegisterUser;
    }

    //// Controller ends

并在视图中:

     // to get id ( IN FORM )
             $form->setAttribute('action', $this->url('user',
                       array('action' => 'updateuser', 'id' => $id)));

/ REDIRECT_URL是您重定向用户的网址, 将UID更改为数据库表中的id,

getUsersTable()是表网关