Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8703021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:47:15+00:00 2026-06-13T02:47:15+00:00

I am using Doctrine and it fails to INSERT data the first time I

  • 0

I am using Doctrine and it fails to INSERT data the first time I do a persist/flush but works the second time, and fails the 3rd time:

// there is no code executed between any of the attempts

$entity = new My\Entity();
$entity->setTag('A');  // just a random field
$em->persist($entity);
$em->flush();
// INSERT not performed
// if I exit here and check the database, no entry is added

$entity = new My\Entity();
$entity->setTag('B');
$em->persist($entity);
$em->flush();
// INSERT performed
// if I exit here and check the database, 1 entry has been added
// and I can see it's "B"

$entity = new My\Entity();
$entity->setTag('C');
$em->persist($entity);
$em->flush();
// INSERT not performed
// if I exit here and check the database, there is still only 1 entry added
// and I can see it's "B"

Here is what I notice on failing attempts:
– There is nothing in the PHP logs (error_reporting is set to all, other Doctrine and PHP issues, including warnings, do show up in the logs).
– The Doctrine SQLLogger doesn’t show anything (on the second attempt it does show the INSERT).

Some troubleshooting steps:
– I wanted to troubleshoot further by replacing the failing attempt with a DQL INSERT query but “INSERT statements are not allowed in DQL” 🙁
– Doing an additional flush before instantiating $entity in the failing attempt doesn’t help
– I can insert as many entries as I want to the database manually and it works, even on first attempt.
– I have the same issue with 2.4.0-DEV.
– I have the same issue with 2.2.2.

I might add that the code is executed inside a PHPunit test, and that in a previous test, I am not experiencing the issue (i.e. Doctrine does properly perform an INSERT on the first persist/flush).

Any idea where the problem might be coming from ?

Version info:
–PHP 5.4
–Doctrine 2.3.0 (pdo_mysql driver)
–MySQL 5.5.24
–Ubuntu 12.04
–PHPUnit 3.7.7

Update 1:

Alright, this is part of the answer. The problem seems to by a routine I use in my PHPUnit setUp() to truncate my database tables between each test:

  • if I truncate my tables between each test, I have problems (i.e. some INSERTs fail).
  • if I don’t truncate, everything works fines.

The way the INSERTs fail seem to be mode random than initially thought, as I created 2 tests of 3 inserts each (and only ran those). When truncating tables between each test, here is what happens to the 3 inserts in each test:
-test 1: SUCCESS / SUCCESS / SUCCESS
-test 2: SUCCESS / SUCCESS / FAILURE (I don’t have FAILURE / SUCCESS / FAILURE like I used to).

Here is the piece of code I’m using to truncate the tables:

$cmd = $em->getClassMetadata($className);
$connection = $em->getConnection();
$dbPlatform = $connection->getDatabasePlatform();
$connection->beginTransaction();
try {
    $connection->query('SET FOREIGN_KEY_CHECKS=0');
    $q = $dbPlatform->getTruncateTableSql($cmd->getTableName());
    $connection->executeUpdate($q);
    $connection->query('SET FOREIGN_KEY_CHECKS=1');
    $connection->commit();
}
catch (\Exception $e) {
    $connection->rollback();
}

I got the code from this SO post and as far as I can see it looks good. I have the same issue if I use this other code:

$connection = $entityManager->getConnection();
$platform   = $connection->getDatabasePlatform();
$connection->executeUpdate($platform->getTruncateTableSQL('my_table', true /* whether to cascade */));

I modified my schema to test with and without the foreign keys and I have the same issue in both cases.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:47:16+00:00Added an answer on June 13, 2026 at 2:47 am

    In the end it appears that the issue occurred when:
    -truncating the tables between each test in the setUp function.
    AND
    -instantiating the Doctrine instance only once in setUpBeforeClass (that I was then accessing with self::$em).

    If I don’t truncate the tables between each test OR if I instantiate my Doctrine instance in setUp, everything works fine.

    I used to like setUpBeforeClass, not such more anymore…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm Natively querying for some data using doctrine using the code below: $sql =
Im using Doctrine and i dont quite understand this code here: $this->hasColumn('id', 'integer', 8,
I am using Agavi with Doctrine. The master template fails to load at times
Possible Duplicate: is there a way to avoid calling nextval() if the insert fails
I'm using Doctrine as ORM in my project but ran against a strange error:
I am using Doctrine and MongoDB for an application and there is one task
Is there any way, when using Doctrine, to create a table like another? I
We're using doctrine migrations and there often are problems when the migration contains multiple
I'm using this tutorial as the basis for a Code Igniter / Doctrine based
I'm currently using the sfDoctrineGuardPlugin and sfForkedDoctrineApplyPlugin and when I reload data using doctrine:data-load

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.