[ask] display error

hi all,i want to ask how to display error from controller?

i have a code like this in controller.php(the controller.php is a global function):


public function aftersave($nama,$tanda,$id,$set=false,$data='')

	{

					

					$sql1 = "select channel_use from tbl_product_category where channel_use = ".$data['channel_use']."";

					$cek = $connection->createCommand($sql1)->queryScalar();

					if($cek == null)

					{

						$sql = "update ".$tabel." set last_user = '".Yii::app()->user->first_name."', first_user = '".Yii::app()->user->first_name."', last_ip = '".$_SERVER['REMOTE_ADDR']."', first_ip = '".$_SERVER['REMOTE_ADDR']."', last_update = NOW(), first_update = NOW(), deleted = 0, `desc`='".$data['desc']."',channel_use = '".$data['channel_use']."' where id = $id";

						$berita = "Create ".$nama." : Name : $current->name, desc : $current->desc, deleted : 0, channel_use : $current->channel_use ";

	

					}				

				else

					{

							$error=1;

							return $error;

					}

	}

and in ProductCategoryController the code is like this :


public function actionCreate()

	{

		$model=new ProductCategory;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

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

			$row = Controller::kueri(2,$model);

			if ($row == null)

			{

				if($model->save())

				{

					$last = Yii::app()->db->getLastInsertID();

					Controller::aftersave("ProductCategory",2,$last);

					$this->redirect(array('view','id'=>$model->id));

				}

			}

			else //if($model->validate())

			{

				$cek = Controller::aftersave("ProductCategory",2,$row,true,$_POST['ProductCategory']);

				if($cek == 1)

				{

					[color="#FF0000"]$this->render('index',array('model'=>$model));[/color]

				}

				else

				{

					$this->redirect(array('view','id'=>$row));

				}

			}

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

and in the _form.php

its already have a coding like this <?php echo $form->errorSummary($model); ?>

i tried this code $this->render(‘index’,array(‘model’=>$model)) but its not working, i have error if i use this code.

thanks.

First of all why do you call manualy the afterSave() method ?

If your controller extends the controller.php then this method will be automatically called after $model->save()…

$model->save() i use this to in controller, buat after save the attribute, i want update some data in db so i use aftersave.

Yes, I got that…

It’s the name afterSave() that confused me… if you have a afterSave() method in the ActiveRecord class than it would be called automatically after $model->save()…

In your case I would suggest you to rename that method just so that you don’t mix it…

So your controller::afterSave() in case of an error returns 1, but in case of no error it does not return anything, and you are not checking the returned value to see if there is some error…

NOTE: moved to proper sub-forum

sorry i ask again,

same code in controller:


$sql1 = "select channel_use from tbl_product_category where channel_use = ".$data['channel_use']."";

					$cek = $connection->createCommand($sql1)->queryScalar();

					if($cek == null)

					{

						$sql = "update ".$tabel." set last_user = '".Yii::app()->user->first_name."', first_user = '".Yii::app()->user->first_name."', last_ip = '".$_SERVER['REMOTE_ADDR']."', first_ip = '".$_SERVER['REMOTE_ADDR']."', last_update = NOW(), first_update = NOW(), deleted = 0, `desc`='".$data['desc']."',channel_use = '".$data['channel_use']."' where id = $id";

						$berita = "Create ".$nama." : Name : $current->name, desc : $current->desc, deleted : 0, channel_use : $current->channel_use ";

	

					}				

				else

					{

						$error=1;

						return $error;

					}

then i return $error to productcategorycontroller like this:


public function actionCreate()

	{

		$model=new ProductCategory;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

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

			$row = Controller::kueri(2,$model);

			if ($row == null)

			{

				if($model->save())

				{

					$last = Yii::app()->db->getLastInsertID();

					Controller::aftersave("ProductCategory",2,$last);

					$this->redirect(array('view','id'=>$model->id));

				}

			}

			else //if($model->validate())

			{

				$cek = Controller::aftersave("ProductCategory",2,$row,true,$_POST['ProductCategory']);

				if($cek == 1)

				{

					$this->render('create',array(

					'model'=>$model,

					'stat'=>1,));

				}

				else

				{

				$this->redirect(array('view','id'=>$row));

				}

			}

		}


		$this->render('create',array(

		'model'=>$model,

		'stat'=>0,

		));

	}

the $error was input to $cek, and if $cek =1 it render to create form, and in the _form i want to adding java script like this :


<div class="row">

		<?php echo $form->labelEx($model,'channel_use'); ?>

		<?php echo $form->textField($model,'channel_use'); ?>

		<?php echo $form->error($model,'channel_use'); ?>

				

		if ($stat = 1)

                  {

                        <script>

			alert("Channel Use Already Taken");

			</script>

                  }			

				

	</div>

but its show error like this : syntax error, unexpected ‘<’

You miss to use the open/close php-tags correctly


<div class="row">

		<?php echo $form->labelEx($model,'channel_use'); ?>

		<?php echo $form->textField($model,'channel_use'); ?>

		<?php echo $form->error($model,'channel_use'); ?>

		<?php // this is missing...

		if ($stat = 1)

                  { ?> // ... and this

                        <script>

			alert("Channel Use Already Taken");

			</script>

              	<?php}?> // ... and this

				

	</div>

thanks, its working now ^^

if i use this code :


<div class="row">

		<?php echo $form->labelEx($model,'channel_use'); ?>

		<?php echo $form->textField($model,'channel_use'); ?>

		<?php //echo $form->error($model,'channel_use'); ?>

		<?php if($stat = 0)

               {

			    echo $form->error($model,'channel_use');

			   }		

			   else

			   {?>

				//how to call the show_alert function??	

			   <?php } ?>

			   

                <script>

		function show_alert()

		{

			alert("Channel Use Already Taken");

		}

	</script>

	</div>




if i using the jvascript without javascript function, the script will always run, even though there is a if condition.

so how to call the javascript function?

thanks

You can do it another way…

In the controller if $cek is 1 (means that you have an error)

You can use addError() - http://www.yiiframew…addError-detail

like:


$model->addError('channel_use','Channel already Taken');

Then in your view file you can just leave


<?php echo $form->error($model,'channel_use'); ?>

i tried that, but its show error no addError method, because addError is in CModel not in controller.

What is the exact code you used?

$model is an instance of ActiveRecord that is extending CModel… so it should work…

sorry, after i refresh it, it working now, thanks.