2

I am using Doctrine/DBAL quite long time, but I have a little problem using executeUpdate and getting the last insert Id. It always returns 0 , but the DB Entry is set. the coloumn id is set and it is AutoIncrement and PRIMARY in my DB.

Here is the function which copies my entry to another table.

$copy = $app['db']->executeUpdate('INSERT INTO `pd_user_model` (
                `campaign_id`,
                ...
                `created`)  
            SELECT `campaign_id`,
                ...                 
                NOW() 
            FROM `pd_pass_model` WHERE `campaign_id` = ?', array($campaignID));

echo "INSERT<br>";
echo "LAST INSERT:" .  $app['db']->lastInsertId(); 


//RETURN VALUE 
INSERT
LAST INSERT:0

Anybody any hints for me. Thanks so far!

4

1 回答 1

4

lastInsertId 返回最后生成的id。在您的用例中,您没有生成 id。您已经为查询提供了 ID,因此 lastInsertId 函数将返回 0,因为它没有生成 ID。

来自MYSQL 文档

LAST_INSERT_ID()(不带参数)返回一个 BIGINT(64 位)值,表示由最近执行的 INSERT 语句为 AUTO_INCREMENT 列设置的第一个自动生成的值,以影响此类列。

于 2015-04-21T20:45:27.203 回答