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 9012735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:04:56+00:00 2026-06-16T03:04:56+00:00

I have a doctrine entity which is being persisted to the database without me

  • 0

I have a doctrine entity which is being persisted to the database without me ever calling persist or flush.

I’ve managed to reproduce the problem quite simply below. As you will see, this script loads a row from a database table called MyEntity and obtains a php object with the row’s fields as attributes. The script then changes one of the attributes, but does not call persist or flush. Then the script create an empty transaction which does nothing. Oddly, this makes the object persist in the sense that the changes made to the php object are also made in the database. I wouldn’t have expected this script to make any changes to the database.

What is happening here?

Note that if I remove the empty transaction, the problem dissapears and no change is made to the database.

Here is the code (test.php):

<?php

    use Doctrine\ORM\Tools\Setup;

    require_once("Doctrine/ORM/Tools/Setup.php");
    Setup::registerAutoloadPEAR();

    $classloader = new Doctrine\Common\ClassLoader('testEntities', __DIR__);
    $classloader->register();

    $paths = array();
    $isDevMode = true;
    $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
    $dbParams = array("driver" => "pdo_mysql",
        "host" => "localhost",
        "user" => "username",
        "password" => "password",
        "dbname" => "databaseName",
        "charset" => "utf8");

    $em = \Doctrine\ORM\EntityManager::create($dbParams, $config);
    $em->getConnection()->exec("SET NAMES UTF8");

    $matches = $em->getRepository('testEntities\\MyEntity')->findBy(array('ID' => 1));
    echo("<pre>");

    if (!empty($matches)) {
        $object = $matches[0];
        print_r($object);
        $object->Content = "WILL IT BLEND?";
        print_r($object);
    }
    $em->transactional(function($em) {
        // Nothing happens here, but if I comment out the call to transactional, the behaviour of the script changes.
    });

    echo("</pre>Done!");
?>

Create and fill MyEntity table in the database:

CREATE TABLE `MyEntity` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Content` varchar(500) COLLATE utf8_swedish_ci NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;

INSERT INTO MyEntity (ID, Content) VALUES (NULL, "Hello World");

Create MyEntity doctrine Entity (testEntities\MyEntity.php):

<?php

    namespace testEntities;

    /** @Entity @Table(name="MyEntity")*/
    class MyEntity
    {
        /**
         * @Id @GeneratedValue(strategy="AUTO") @Column(type="integer") 
         */
        public $ID;

        /**
         * @Column(type="string", length=500)
         */
        public $Content;
    }

?>
  • 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-16T03:04:58+00:00Added an answer on June 16, 2026 at 3:04 am

    Doctrine manual: 9. Transactions and Concurrency -> 9.1.2. Approach 2: Explicitly (at section bottom):

    The difference between Connection#transactional($func) and
    EntityManager#transactional($func) is that the latter abstraction
    flushes the EntityManager prior to transaction commit and also closes
    the EntityManager properly when an exception occurs (in addition to
    rolling back the transaction).

    So: EntityManager#transactional($func) flushes the EntityManager prior to transaction commit (if $func call was successful – your case).

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

Sidebar

Related Questions

I have an Attachment Entity in Doctrine which references a file on Amazon S3.
I have setup a doctrine listener which is fired on different database actions and
I'm encountering a problem with a Doctrine 2 entity association. I have a User
I have a Doctrine entity Page which is loaded by module Foo . The
In my Doctrine entity, which is data_class for my form I have a file
I have an entity Movie which has a unique constraint through doctrine annotation. Based
I have a Doctrine Entity (News) which I listen for the event prePersist .
I have a service in which I inject the entity manager service (@doctrine.orm.entity_manager) because
I have a number of simple controller classes that use Doctrine's entity manager to
I have a 'vanilla' install of CodeIgniter 2 + Doctrine 2. My tutorial Entity.User.dcm.yml

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.