Transactions
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 237
- Forks
- 32
- PR merge metrics
- No merged PRs in 30d
Description
Hi Again!
I was wondering how can I work with transactions like Rollback and Commit.
I've checked that the flush() method already deals with it, but here's what I'm trying to accomplish:
Let's say I have the following database structure:
sales (id INT AUTO_INCREMENT, date TIMESTAMP) sales_item (id INT AUTO_INCREMENT, sales_id INT, products_id INT, value FLOAT ) products (id INT AUTO_INCREMENT, description VARCHAR(255) )
I want to save a sale in one click. So here's what I'm trying to do:
$sale = new \stdClass;
$sale->date = new date('Y-m-d H:i:s');
$mapper->persist($sale);
// Here is the tricky part
// I need the $sale->id to do this, see:
$sale_item = new \stdClass;
$sale_item->sales_id = $sales->id;
$sale_item->product_id = 1;
$sale_item->value = 10;
$mapper->persist($sale_item);
$mapper->flush();
The problem is that $sales->id is NULL as the mapper wasn't flushed at that point. I know I can flush it and get the ID I want, but if anything happens and I can't save the sale_item, the sale would be already commited, which means I'd have to delete it and flush the database again. Is it possible to achieve what I want?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the mapper's flush() method, the only entry point named in the issue, and trace how it handles generated IDs and commit behavior. Done means establishing whether the two persists can share one transaction and documenting or scoping the required change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100