How to trap duplicate value inserted in database?

Hello to all, I have a problem again in inserting in database, I don’t know how to check in the data inserted in database if its already exist, It’s any way in yii easily check the database if the data exist or not? thanks a lot in advance sir/maam :lol:

Hi there,

Yes, there is an easy way to check if data sent to the model is unique. In the model sample file you’ll see a block of code like this (pay attention to the last array):

public function rules()


		return array(

			// username and password are required

			array('username, password', 'required',

				'message'=>'Este campo no puede estar vacío.'),

			// rememberMe needs to be a boolean

			array('rememberMe', 'boolean'),

			// password needs to be authenticated

			array('password', 'authenticate'),

                        // This is the option you need

                        array('username', 'unique',

				'message'=>'The username you have selected already exists in the database.'),



Notice that the ‘username’ field is being checked against database to find out whether it is ‘unique’. In case it’s not, a message will be returned.



:lol: :lol: :lol:

thanks a lot sir ,it’s working

That’s good news. Hope to be of help in the future. Please mark the topic as answered and add a reputation point.