我目前正在处理的项目有问题并且 这是我的注册 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/