php - 如何在zend中获取新插入用户的主键值

标签 php mysql zend-framework

我目前正在处理的项目有问题并且 这是我的注册 Controller 每次用户注册时,他们的个人资料中都会显示一个默认图像

      public function registerAction()
       {
      $form = new Application_Form_Users();
    $form->submit->setLabel('Register');
    $this->view->form = $form ;

    if ($this->getRequest()->isPost()) {

    $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $id = $form->getValue('uid');
            $firstname = $form->getValue('firstname');
            $lastname  = $form->getValue('lastname');
            $email     = $form->getValue('email');
            $username  = $form->getValue('username');
            $password  = $form->getValue('password');
            $vpassword = $form->getValue('vpassword');

            if ($password == $vpassword) { 
            $register = new Application_Model_DbTable_Users();
            $password = md5($password);
            $register->addUser($firstname , $lastname , $email , $username , $password );


            if ($register) {
                        $register = new Application_Model_DbTable_Users();
                        $uid = $form->populate($register->getUser($id));

                        $addimg = new Application_Model_DbTable_Images();
                        $imagepath = APPLICATION_PATH.'/../public/upload/';
                        $addimg->addImage($uid , $imagepath);
                    }
            $this->_helper->redirector('index');
            } else { 
            $this->view->errorMessage = "Passwords don't match.";
            }
            } else {
            $form->populate($formData);
            }
        }

}

这是我将默认图像路径添加到数据库中的函数

            public function addImage($uid , $imgpath)
    {
        $data = array(
        'uid'      => $uid ,
        'imgpath'  => $imgpath ,
        );
        $this->insert($data);
    } 

但是我收到错误,因为我的 uid 为 null 我的问题是如何获取用户表中的 uid 值,用户表和图像表也有关系。

最佳答案

如果您使用的是Zend_Db_Table,那么您可以在addUser()中执行类似的操作:

public function addUser($firstname , $lastname , $email , $username , $password ){
    $user = $this->createRow();
    $user->fname = $firstname;
    $user->lname = $lastname;
    ...
    $user->save();
    return $user->id;
}

这将返回您的用户 ID,以便您可以执行以下操作:

$id = $register->addUser($firstname , $lastname , $email , $username , $password );

$addimg->addImage($id , $imagepath); //$id, not $uid !

关于php - 如何在zend中获取新插入用户的主键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23903297/

相关文章:

php - 如何从 PHP 文件中的 jQuery.getJSON 获取数据传输的数据?

php - 如何在 PHP 页面上显示 hg 存储库的当前工作副本版本

php - Zend Framework 中的DI 2. 如何建立循环依赖?

mysql - WHERE 子句中的 IF 语句

php - 与 Joomla 的事件 SQL 连接

zend-framework - Doctrine2或zend_db

php - Lucene 中的多术语通配符查询?

php - PHPSpec/Prophecy 中通过引用传递的形式参数 stub

Php exec 函数没有按预期工作

mysql - SQL按日期分组问题