Skip to content

Transactions #51

@jonyhayama

Description

@jonyhayama

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions