Yii Framework Forum: How To Deal With Cdbexceptions - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Deal With Cdbexceptions Sorry if this is obvious. Rate Topic: -----

#1 User is offline   Jonny 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 184
  • Joined: 26-December 11

Posted 17 August 2013 - 11:10 PM

Hi,

I'm using AR to save a record. I have a duplicate key contraint on a couple of columns.

When i use:
			$model = new Record;
			$model->user_id = (int) $id;
			$model->friend_id = (int) Yii::app()->user->id;
			
			if($model->save()){
 				
				echo 'Record Added</div>';
		
			} else {
			
				echo 'Something went wrong';
				
			}


This works fine, but not if Integrity constraint violation occurs. What's the correct way to deal with this.

try and catch?
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,701
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 18 August 2013 - 12:10 AM

The correct way is to understand why do you have this constraint, whether you really need it and why duplicate key situation occurs.

If it's ok to you (constrains really must exist and users shouldn't submit such data) consider using "unique" validator in your model.
God is real unless declared as integer
1

#3 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 18 August 2013 - 02:07 AM

From the UI designer standpoint you shouldn't allow the user to perform actions that will cause errors. This is an example when you know that might happen and can prevent it.
For example, if a user is choosing both values from dropdown lists you should filter their contents in such way that choosing an existing pair is impossible.

A little off topic:
The same goes for action buttons, if it can't be performed don't let user click the button and then display an error message, just make that button disabled, optionally with a note next to it or in a popover why it is so.
Don't be a dick.
1

#4 User is offline   Jonny 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 184
  • Joined: 26-December 11

Posted 18 August 2013 - 10:55 AM

Thank you. I actually had answered this in a previous situation and forgot about it.

I used http://www.yiiframew...utes-validator/

Thanks for setting me straight.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users