Uncaught exception duplicate key 1 for primary ADODB

Permalink
Help,

I'm building a package for C5. I was using an auto-increment primary key 'eID' in the database. ADODB falls down with auto-increment though, so I wrote a small function to handle that.

Now, ADODB tries to INSERT a new record when it should UPDATE one.

My save() function calls...

parent::save($args);


Can anyone help? PS, I've looked extensively at the C5 forums and found these answers, but they did not solve the problem:

http://www.concrete5.org/community/forums/chat/adodb_exception-dupl...
and
http://www.concrete5.org/community/forums/installation/error-upgrad...

My primary key structure looks like this:

`eID` int(10) unsigned NOT NULL DEFAULT '0'

How can I tell ADODB to UPDATE, not INSERT. Any help is soooo appreciated, thanks!
J

Jongosi
 
Jongosi replied on at Permalink Reply
Jongosi
Here's an answer (sort of) for anyone else who stumbles upon this.

1. I removed all the auto-incrementing from the database. C5 creates a new instance of a block on `UPDATE` so ADODB will actually perform an INSERT with a new bID (ie: a new version of that block).

2. I set the bID as the primary key and eID as an int(11) field, unsigned null.

3. I created a simple function in the controller to do the heavy lifting. In the controller:

$this->eID = 0;
public function generateRandNum() {
    if (intval($this->eID)>0) {
        return $this->eID;
    } else {
        return intval(mt_rand(1,time()));
    }
}


4. In the add.php and edit.php files, we call the function like so:

<?php $obj= $controller; ?>
<input type="hidden" name="eID" value="<?php echo $obj->generateRandNum()?>" />


5. Done.

On `add` we get a new random number generated and on `edit`, the eID is retrieved from the DB.

It has to be said this could theoretically be done another way, as JordanLev has pointed out to me. Unfortunately, I couldn't get it to work that way, but feel free to check out the train of events here:http://stackoverflow.com/questions/12382362/php-concrete-5-pass-var...

I hope that helps someone out there.