tags:

views:

685

answers:

7

I'm running in to the issue below when trying to run the following:

$store = new Store();
$store->url =$this->form_validation->set_value('website');
$store->save();
$store_id = $store->identifier();


Fatal error: Uncaught exception 'Doctrine_Connection_Exception' with message 'Couldn't get last insert identifier.' in /home/yummm/public_html/system/application/plugins/doctrine/lib/Doctrine/Connection/UnitOfWork.php:932 Stack trace: #0 /home/yummm/public_html/system/application/plugins/doctrine/lib/Doctrine/Connection/UnitOfWork.php(632): Doctrine_Connection_UnitOfWork->_assignIdentifier(Object(Category_store_assn)) #1 /home/yummm/public_html/system/application/plugins/doctrine/lib/Doctrine/Connection/UnitOfWork.php(562): Doctrine_Connection_UnitOfWork->processSingleInsert(Object(Category_store_assn)) #2 /home/yummm/public_html/system/application/plugins/doctrine/lib/Doctrine/Connection/UnitOfWork.php(81): Doctrine_Connection_UnitOfWork->insert(Object(Category_store_assn)) #3 /home/yummm/public_html/system/application/plugins/doctrine/lib/Doctrine/Record.php(1691): Doctrine_Connection_UnitOfWork->saveGraph(Object(Category_store_assn)) #4 /home/yummm/public_html/system/application/controllers/auth.php(375): Doctrine_Reco in /home/yummm/public_html/system/application/plugins/doctrine/lib/Doctrine/Connection/UnitOfWork.php on line 932

When I echo $store_id, it seems to be grabbing the last id without any issues. Any idea why this error keeps coming up even though the ID is passing correctly?

A: 

Maybe you overwrote the setTableDefinition() Method, and now there is no hasColum() call for your id attribute

guest
A: 

I have the same issue in my project, but the same web application on other computers work correctly without any errors. It is very strange and i still don't know why it happens (have no time to search the answer).

Andy
A: 

I have found that the problem is in 'autoincrement' => true for primary key field inside hasColumn(). If I set 'autoincrement' => false, the problem is gone.

But the primary key field of that table is an autoincremented field. And it is still strange, that on the others computers there are no any errors. The application and databases are the same on all that computers.

Andy
+1  A: 

And now i have found that it is because lastInsertId() method of PDO object returns 0. So, suppose, the problem is in PDO driver and it is not the Doctrine bug. Need to reinstall PHP or PDO extension. Also, see http://bugs.php.net/bug.php?id=33618.

Andy
this bug report has been closed for five years. The PDO drivers should work properly.
Thomas
+1  A: 

It's possible that the column definition itself isn't set up as auto_increment. Check the table definition with: (assuming MySQL)

DESCRIBE table_name;

and make sure that your ID field is flagged as auto_increment. If it's not Doctrine will try to get the last identifier for the table but will fail because only auto_increment columns response to MySQL's LAST_INSERT_ID() function.

Thomas
A: 

Your ID from Store table is probably not set to autoincrement. In this case, you have to 'tell' it to doctrine record model by used mixed option parameter in the hasColumn function:

$this->hasColumn('id', 'integer', 11, array(
    'primary' => true,
    'autoincrement' => false) 
    );

Or set your ID autoincrement if needed.

oL
A: 

I have the same problem and it was the auto increment definition in the table.

Gerardo Jaramillo