[solved] MySQL integrity check

Hello,

I have one question here :)

How do You solve MySQL parent table deletion issue??

I parent table has child table records, it throws CDbException error, it would be nice to warn user about it before an ungly error :) What are your options?:)

Thanks for help in advance!

CoLT

i think


public function beforeDelete(){

  if(count($this->childs)>0){

    /// ... do smth

    return false;

  }

  return parent::beforeDelete();

}

in model class :rolleyes:

Right this is very simple - just does nothing ;) But how to inform the user friendly and simple?


/// ... do smth

:)

[edit] By the way, shouldn’t this go to Controller? Also I use multiple views where delete is called, so maybe pop up promt would be the best way to inform user?

Thanks

CoLT

You could also wrap your AR calls in try/catch blocks in the controller to catch the exception and do something nice.

Any example ?;)


try {

   $model-save();

} catch(CDbException $e) {

   echo "Don't do that!!";

}



Yeah, all methods kind of solve the problem, but I need an example of right message to inform the user about the problem - Parent table ‘PTable’ has a record in child table ‘CTable’.

In my default controler Branches are parent table, courses are Child table.




...

	public function actionDelete()

	{

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

			$this->loadModel()->delete();


			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_POST['ajax']))

				$this->redirect(array('index'));

		}

		else

			throw new CHttpException(400,Yii::t('App','Invalid request. Please do not repeat this request again.'));

	}

...



After try catch I miss the CDbException but how to I properly inform user? E.g. In popup or message and discontinue redirect to index?

Thanks

Quick idea: create actionConfirmDelete() that expects id of parent in $_GET. It shows a page with all children and a button "Really delete these?"

You can redirect to that action if an Exception occurs.

I do it like that, pretty close to Mike’s solution <_<


try{

  $model->save();

} catch (Exception $e) {

  $model->addError(null, $e->getMessage());

}

Shows the error in the ‘errorSummary’.

Available in phundament CRUD controller template.

Hm, here I’m a bit stuck with Delete.

E.g. in ModelController


	

	public function actionDelete()

	{

 ///Integrity constraint violation SEARCH

		if(Yii::app()->request->isPostRequest)

		{

		try{

     $this->loadModel()->delete();

       if(!isset($e)){

  			  if(!isset($_POST['ajax']))

  			  $this->redirect(array('index'));

  		 }

		} catch (Exception $e) {

			  	echo 'how to implement pop up';

			} //catch end

		}

		else

			throw new CHttpException(400,Yii::t('App','Invalid request. Please do not repeat this request again.'));

	}



How could I implement pop up here with Yii logic in the same page instead of deletion error (now opens new window with ugly message :D ) ?

Thanks for help ;)

CoLT

[solved - add better solution if any :) ]

Just added variable to check if error occurred and added CJuiDialog explaining the situation to user ;)


	public function actionDelete()

	{

		$error_del=0;

		if(Yii::app()->request->isPostRequest)

		{

		try{

     $this->loadModel()->delete();

       if(!isset($e)){

  			  if(!isset($_POST['ajax']))

  			  $this->redirect(array('index'));

  		 }

		} catch (Exception $e) {

			  	        if(isset($e)) $error_del=1;

                                        if(!isset($_POST['ajax']))

					$this->redirect('index',array(

					'error_del'=>$error_del,

					));

			} //catch end

		}

		else

			throw new CHttpException(400,Yii::t('App','Invalid request. Please do not repeat this request again.'));

	}

Other solutions are welcome :) Like to add a pop up in admin grid view :) now it just ignores the parent-child error.

I was having troubles sending error_del variable to Index view, I used get method instead (tested and works):


					$this->redirect(array('index',

					'error_del'=>$error_del,

					));



Greetings,

CoLT

What solution would you offer to MyISAM MySql DB engine as it does not support fk constraints?

Best regards,

CoLT