Jump to:

23575 Posts in 19403 Topics by 2893 members

General Questions

SilverStripe Forums » General Questions » Handling error when having a "Unique" key index

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 383 Views
  • Zyzzza
    Community Member
    25 Posts

    Handling error when having a "Unique" key index Link to this post

    Hi All,

    Anyone could advise - I've got a Unique index on Dataobject like

    static $indexes = array(
    "UniqName" => array(
    "type" => "unique",
    "value" => "`URL`"

    When i try writing a Dataobject with $item->write() it would give me a n error "[User Error] Couldn't run query: UPDATE... " ... Duplicate entry 'http://google.com' for key 2.

    I was pinpointed by UncleCheese over the IRC that error handling is :

       public function databaseError($msg, $errorLevel = E_USER_ERROR) {
    user_error($msg, $errorLevel);

    So probably there wont be easy solution like try...catch or smth.

    But if you have found a solution for this - please post it here .

    PS - I cannot check for duplicates on each write, as there's over 100K records, and doing so would just kill the script .

  • capsula4
    Community Member
    4 Posts

    Re: Handling error when having a "Unique" key index Link to this post

    Although is not the cleanest solution, it does the trick when you don't want to perform a verification query.

          $error = ob_get_contents();
          if(empty($error) && strpos($error, 'duplicate key') !== false){
             return true;
             return false;

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.