Fatal MySQL Error

Permalink
Wondering if someone can help me determine why I am getting a MySQL fatal error when saving edits to an instance of a custom block I am working on based off of the slideshow block - I'm using it as base to figure out how to build blocks with multiple tables.

Error:
Fatal error: Uncaught exception 'ADODB_Exception' with message 'mysql error: [1062: Duplicate entry '12' for key 'PRIMARY'] in EXECUTE("INSERT INTO btMyBlock (bID) VALUES (12)") ' in /Users/username/Sites/temp.dev.domain.com/concrete/libraries/3rdparty/adodb/adodb-exceptions.inc.php:78 Stack trace: #0 /Users/username/Sites/temp.dev.domain.com/concrete/libraries/3rdparty/adodb/adodb.inc.php(1037): adodb_throw('mysql', 'EXECUTE', 1062, 'Duplicate entry...', 'INSERT INTO btM...', false, Object(ADODB_mysql)) #1 /Users/username/Sites/temp.dev.domain.com/concrete/libraries/3rdparty/adodb/adodb.inc.php(1012): ADOConnection->_Execute('INSERT INTO btM...', false) #2 /Users/username/Sites/temp.dev.domain.com/concrete/libraries/3rdparty/adodb/adodb-lib.inc.php(212): ADOConnection->Execute('INSERT INTO btM...') #3 /Users/username/Sites/temp.dev.domain.com/concrete/libraries/3rdparty/adodb/adodb.inc.php(1658): _adodb_replace(Object(ADODB_mysql), 'btMyBlock', Array, Array, false, fa in /Users/username/Sites/temp.dev.domain.com/concrete/libraries/3rdparty/adodb/adodb-exceptions.inc.php  on line 78


It looks to me like the BlockController::save method is trying to resave the primary Block record in the database over the existing record using the same bID after calling parent::save from my custom block. When I disable the call, the error obviously disappears.

It might help to know that currently the only column in the primary database table at this point in its development is the bID column ... not sure if that might be partly the reason.

The slideshow block I'm basing the basic structure of my custom block on obviously does not trigger this error, but I don't currently know enough about the underpinnings of Concrete5 to determine why my block is failing. Saving data to my second table works fine regardless of the error.

Here is my custom save method:
function save($data) { 
   // load database functionality
   $db = Loader::db();
   // remove all key records for this block instance
   $db->query("DELETE FROM btMyBlockKeys WHERE bID=" . intval($this->bID));      
   // store returned keys
   $keyCount = 0;
   foreach($data['mbKeys'] as $key){
      // don't add the template
      if($data['mbNames'][$keyCount] == 'tempName') continue;
      // add records for block instance
      $db->Execute('INSERT INTO btMyBlockKeys (bID, mbName, mbKey) values (?, ?, ?)', array(intval($this->bID), $data['mbNames'][$keyCount], $key));      
      // increment key count
      $keyCount++;
   }


Any thoughts would be helpful. Thanks in advance for any help given!

dwhillier
 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
I don't know for sure what the problem is, but here are some ideas:

* It may just be a weird occurrence that it failed that one time -- perhaps something strange like saving, then hitting the back button and re-saving (I'm not saying this would definitely cause that behaviour -- just throwing some ideas out there).

* Your save method is trying to save keys to the secondary table before it calls the parent's save() method, which might cause problems if this is saving a NEW block (because there's no $this->bID yet) -- try moving the parent::save($args) call up to the top of the function.

* I think I read somewhere that you actually need to have TWO fields in your block's primary table -- a bID and something else (anything else, like a dummy text field, you can even call it "dummy" if you want as it does not need to be used by anything, it just needs to exist). Not sure why, but worth a shot.

Good luck!

-Jordan Lev
JimboJetset replied on at Permalink Reply
JimboJetset
can you post the db.xml contents for the btMyBlockKeys table as this may hold a clue.
dwhillier replied on at Permalink Reply
dwhillier
"I'll take what's behind option number 3!"

I added a couple extra columns to the primary block table and voila, all is now good!

Thanks guys for taking the time to respond!
beingalex replied on at Permalink Reply
Option 3 worked. You saved my life.