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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:12:59+00:00 2026-05-17T15:12:59+00:00

I have two complicated PHP objects, each of which has data in a few

  • 0

I have two complicated PHP objects, each of which has data in a few MySQL tables.

Sometimes, I just need to remove one object A from the database, and that takes 3 SQL statements.

Sometimes, I need to remove one object B from the database, which is takes 4 SQL statements, and which also needs to find and remove all of the object A’s that object B owns.

So inside the function delete_A(), I execute those statements inside of a transaction. Inside of the function that delete_B(), I want to run one great big transaction that covers the activities inside of delete_A(). If the whole atom of deleting a B fails, I need to restore all of its A’s in the rollback.

How do I update the definition of delete_A() to only open a new transaction if there isn’t already a bigger transaction running.

I expected to be able to do something like this, but the autocommit attribute doesn’t appear to get changed by beginTransaction()

function delete_A($a){
  global $pdo;
  $already_in_transaction = !$pdo->getAttribute(PDO::ATTR_AUTOCOMMIT);
  if(!$already_in_transaction){
    $pdo->beginTransaction();
  }

  //Delete the A

  if(!$already_in_transaction){
    $pdo->commit();
  }
}
function delete_B($b){
  global $pdo;
  $pdo->beginTransaction();
  foreach($list_of_As as $a){
    delete_A($a);
  }
  $pdo->commit();
}
  • 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-05-17T15:13:00+00:00Added an answer on May 17, 2026 at 3:13 pm

    I ended up just using the exception thrown by ->beginTransaction() to figure out whether I was in a transaction, and using that to decide whether to commit in the inner loop. So delete_A() ended up looking like:

    function delete_A($a){
      global $pdo;
      try {
        $pdo->beginTransaction();
      } catch (PDOException $e) {
        $already_in_transaction = true;
      }
    
      //Delete the A
    
      if(!$already_in_transaction){
        $pdo->commit();
      }
    }
    

    And delete_B() works without modification.

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

Sidebar

Related Questions

I have two entities: Project, Employee Employee has primary key {employeeId} + some other
I have two websites (one asp classic and the other asp.net) which we would
I have quite a complicated ListView . Each item looks something like this: >
In my listview object insert/edit template, I have two dropdownlists (category, subcategory). When a
I have a simple polygon (convex or concave, but no holes) that I need
Somewhere in my code I have: class aclass { ... function amethod() { $this->dom
For reasons which are somewhat unavoidable (lots of legacy code, compatibility, design needs) I
I have a bunch of hidden images on my website. Their container DIVs have
I have implemented a Facebook Like button into my site. I wanted to make
Some time ago we needed a solution for Single Sign On authentication between multiple

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.