我目前正在处理的项目有问题,
这是我的寄存器控制器
每次用户注册时,都会在其个人资料中显示默认图像
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/