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

时间:2014-05-28 05:17:05

标签: php mysql zend-framework

我的项目目前正在处理中存在问题 这是我的注册控制器 每次用户注册时,都会有一个将在其个人资料中显示的默认图像

      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我的问题是如何在我的users表中获取uid的值,还有用户表和图像表有关系。

4 个答案:

答案 0 :(得分:0)

您可以在数据库中为每个新条目设置$ imgpath的默认值。

答案 1 :(得分:0)

您可以使用lastInsertId()类中的Zend_Db_Adapter功能。

http://framework.zend.com/manual/1.12/en/zend.db.adapter.html#zend.db.adapter.write.lastinsertid

在你的情况下应该是:

$this->getAdapter()->lastInsertId()
addImage方法的最后一行之后

警告:这不适用于所有SQL适配器(如PostgreSQL)

答案 2 :(得分:0)

从我可以从您的代码中获得的内容,您将添加一个新用户,然后添加与其ID相关的图像。

 $uid = $form->populate($register->getUser($id));
 $addimg = new Application_Model_DbTable_Images();
 $imagepath = APPLICATION_PATH.'/../public/upload/';
 $addimg->addImage($uid , $imagepath);

并且,您收到“错误”,因为$ uid为null。但是$ id不应该为null,它应该是您刚刚添加的用户的ID。

所以你可以尝试一下,看看它是否适合你

$addimg->addImage($id , $imagepath);

答案 3 :(得分:0)

如果您使用的是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 !