views:

69

answers:

2

I am novice to joomla. I am using Joomla 1.5. I have declared a field in database table as unique. Lets suppose , I have declared "username" as unique . I created a component named "com_user" in the administrator/components section. Now, when I add a user "xyz123", for the first time, its ok. But, during next entry, when I enter the same value, it should generate error with displaying error message showing "Username should be unique" or sth. like this . How can this be done?

A: 

The best way to do this I think is to just do a "select count(*) from..." before you try to insert the user into the database to see if the user already exists. The best way to display errors, warnings and notices would be to use the JError class

A second way is to use the getErrorNum() and getErrorMsg() methods of the DBO object. More information on that here: http://www.joomladin.com/index.php/tutorials/34-module-development/54-exception-handling.html Although I do believe that while using this second method the user still might see a big red "Database Error: duplicate key..." message.

klennepette
A: 

The store() method of your JTable class should return false when the query fails, so you could use this to determine when a duplicate record is being entered. However, the store() method will also return false when the query fails for other reasons (lost database connection, etc...).

jlleblanc