开发者

SOlving the problem of unique key in joomla

开发者 https://www.devze.com 2022-12-21 23:20 出处:网络
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 t

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?


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.


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...).

0

精彩评论

暂无评论...
验证码 换一张
取 消