Login: code before $this->redirect(Yii::app()->homeUrl);

Hi all,

When putting $activity->save(); as shown below before $this->redirect(Yii::app()->homeUrl);, the site breaks when log in details are incorrect.

However, $activity->save(); works just fine in other actions such as actionLogout() below.

What went wrong?

Thanks,

Michael




public function actionLogin()

	{

		/* log in */

		

		$model=new LoginForm;


		// if it is ajax validation request

		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}


		// collect user input data

		if(isset($_POST['LoginForm']))

		{

			$model->attributes=$_POST['LoginForm'];

			// validate user input and redirect to the previous page if valid

			if($model->validate() && $model->login())										

				

				

				// log

				$activity = new Activity;

				$activity->userId = 1;				

				//$activity->userId = Yii::app()->user->id;

				$activity->name = "logging in";

				// This breaks the site when log in details are wrong

				$activity->save();

				

				$this->redirect(Yii::app()->homeUrl);

								

				

				

		}


		// display the login form

		$this->render('login',array('model'=>$model));

	}


	/**

	 * Logs out the current user and redirect to homepage.

	 */

	public function actionLogout()

	{

		


		// log

		$activity = new Activity;

		$activity->userId = Yii::app()->user->id;

		$activity->name = "logging out";

		$activity->save();	

		

		

		Yii::app()->user->logout();					

		

		$this->redirect(Yii::app()->homeUrl);

	}






You are missing here some brackets…

if the details (credentials) are invalid that part of the code should not be executed at all…

what happens in you rcode is that in case of wrong credential the line $activity = new Activity; is not executed… but the next lines are… and they of course break your app becasue the model is not created…

Thanks, that worked.