Weird problem with _forward

For programming and general questions on Zend Framework
Post Reply
snorri67
Posts: 7
Joined: Thu Aug 27, 2009 2:22 pm

Weird problem with _forward

Post by snorri67 » Mon Sep 20, 2010 10:22 am

Hello,
I've got a registerAction in my UserController which works perfectly apart from the forwarding to a success page.
The weird thing about it is that if I comment out the line which executes the model function (l.17) and assign a newUserId manually (l.18), the _forward works again!
There are no exceptions or errors anyway, and everything works (DB row inserted, mail sent), but afterwards I see the blank register form again...
I am completely lost on this.

Here is my controller code:

Code: Select all

public function registerAction ()
	{
		$usersTable = new Model_User();
		$usrRegForm = new Form_User();
		$usrRegForm->setAction('/user/register');
		$usrRegForm->removeElement('role');
		$usrRegForm->addElement('hidden', 'role', array('decorators' => array('ViewHelper')));
		$usrRegForm->getElement('username')->addDecorator(array('ajaxDiv' => 'HtmlTag'), array('tag'=>'div', 'placement'=>'append', 'id'=>'username_help', 'class'=>'errors'));
		$usrRegForm->getElement('email')->addDecorator(array('ajaxDiv' => 'HtmlTag'), array('tag'=>'div', 'placement'=>'append', 'id'=>'email_help', 'class'=>'errors'));
		$usrRegForm->role->setValue('user');
		if ($this->getRequest()->isPost()){
			if ($usrRegForm->isValid($_POST)){
				$values = $usrRegForm->getValues();
				try{
					$activationCode = sha1(uniqid('xyz', true));
					$values['activation'] = $activationCode;
					$newUserId = $usersTable->saveUser($values);
//					$newUserId = 2;
					// send activation email
					require_once( BASE_PATH . '/library/Mailer/Mailer.php');
					try{
						$mailer = new Mailer();
						$languageCode = 'de';
						$activationLink = "http://" .$_SERVER['HTTP_HOST']. '/user/activate/id/' . $newUserId . '/ac/' . $activationCode;
						$mailer->sendRegistrationMail($values['email'], $values['username'], $activationLink, $languageCode);

					} catch (Exception $e){
						die ("Problem in User-Model->Mailerinstantiierung : " . $e->getMessage());
					}
						
					$this->view->userJustRegistered = $values['username'];
					//						$this->view->userid = $newUserId;
					$this->view->username = $values['username'];
					return $this->_forward('activate');

				}
				catch (Zend_Db_Statement_Exception $e){
					// race condition can occure between validator and actual DB operation
					// so check if username or email is already registered
					// NOTE: this should be very rare case, because we check this in validators just before inserting the row
					$message = $e->getMessage();
					if (strpos($message, $values['email']) !== false)
					{
						//email already registered
						$this->view->globalPageError = 'email';
					}
					else if (strpos($message, $values['username']) !== false)
					{
						//username already registered
						$this->view->globalPageError = 'username';
					}
					else
					{
						// other error, rethrow the exception
						die($e->getMessage);
					}
				}


			}
		}

		$this->view->headScript()->appendFile('/js/jquery-1.4.2.min.js')
		->appendFile('/js/fancybox/jquery.mousewheel-3.0.2.pack.js')
		->appendFile('/js/fancybox/jquery.fancybox-1.3.1.pack.js')
		->appendFile('/js/form.js');
		$this->view->headLink()->appendStylesheet('/js/fancybox/jquery.fancybox-1.3.1.css');
		//		$this->view->script = $script;
		$this->view->Form = $usrRegForm;
	}
The model function:

Code: Select all

public function saveUser(array $data)
	{
		$data['password']  = $this->generatePw($data['password']);
		$data['registerDate'] = new Zend_Db_Expr('NOW()');
		$fields = $this->info(self::COLS);
		foreach ($data as $field => $value)
		{
			if (!in_array($field, $fields)) {
				unset($data[$field]);
			}
		}
		return $this->insert($data) ;
	}

Thanks for any help in advance.

snorri67
Posts: 7
Joined: Thu Aug 27, 2009 2:22 pm

Re: Weird problem with _forward

Post by snorri67 » Mon Sep 27, 2010 11:15 am

Now I found that the call of a private function in my model affects the behaviour of _forward in the controller...
If I comment out the line which encrypts the password, everything works fine again...

Code: Select all

  $data['password']  = $this->generatePw($data['password']);
I can't save the password without encryption off course, so this does not help.
Any suggestions?

manojdharg
Posts: 4
Joined: Tue Sep 28, 2010 11:41 am

Re: Weird problem with _forward

Post by manojdharg » Tue Sep 28, 2010 12:08 pm

Hi Snorri,

Try $this->_forward('controller/action') with out return, hopefully it should serve the purpose.

Thanks
Manooj

snorri67
Posts: 7
Joined: Thu Aug 27, 2009 2:22 pm

Re: Weird problem with _forward

Post by snorri67 » Wed Sep 29, 2010 11:33 am

Thanks for your suggestion. But this changes nothing.
I have read somewhere _forward should always be used with return.
Now I replaced the _forward with a _redirect and this works fine.
I can't use the view-variables this way, but in this case I can live with that.

Post Reply