Relational icon indicating copy to clipboard operation
Relational copied to clipboard

Transactions

Open jonyhayama opened this issue 11 years ago • 7 comments

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?

jonyhayama avatar Dec 20 '13 12:12 jonyhayama

I've just accomplished it! instead of using

$sale_item->sales_id = $sales->id;

I've used

$sale_item->sales_id = $sales;

May I just say it is very simple and elegant solution you guys have there? Perhaps it could go on the documentation? Each day I'm loving Respect\Relational more! :)

jonyhayama avatar Dec 20 '13 13:12 jonyhayama

Hello @jonathanhayama you can do yourself ;)

williamespindola avatar Dec 21 '13 02:12 williamespindola

I have a similar problem, but I believe it is a bit more complex than that. Households need to save objects that have different identifier (ID only and different from, or "table name" + id .. etc, etc) they have specific identifiers of an internal standard. something like: tb_people, identifier: ppl_id or something. From what I can find out is not possible "to set" An identifier for each object to be persisted ... Can anyone give a suggestion?

cristopher-rodrigues avatar Jan 12 '14 13:01 cristopher-rodrigues

@cristopher-rodrigues the base component for Relational, Respect\Data, implements the concept of styles: naming conventions that describe how the database is structured. It is a mixture of configuration and convention (you can choose which convention to follow). It is an unfinished feature (it works, but is split in two repositories) and since your problem seems to be related to naming conventions, I guess it could solve your problem:

Here is the NorthWind style sample: https://github.com/Respect/Data/blob/master/library/Respect/Data/Styles/NorthWind.php. NorthWind is the sample database for the Microsoft SQL Server 2000, many people use that standard so we implemented it.

Unfortunately, when I moved styles to Respect\Data I've been unable to move the tests as well, so the NorthWindTest is available here: https://github.com/Respect/Relational/blob/develop/tests/library/Respect/Relational/Styles/NorthWindTest.php

These tests show how the naming conventions for these databases are different. The NorthWind class simply translates the identifiers (changing case and plurals), but you can make the methods more complex:

<?php
class MyCustomStyle extends Standard

    protected $pks = array('tb_people' => 'ppl_id', 'sales' => 'slid');

    // Gets a primary key name    
    public function identifier($name)
    {
        return isset($this->pks[$name]) ? $this->pks[$name] : parent::identifier($name);
    }

}

Check out the tests for other styles to understand how each interface method works (identifier, remoteIdentifier, etc).

alganet avatar Jan 12 '14 14:01 alganet

@jonathanhayama would you like to write the docs section for that feature? =) There are some tests here for reference:

https://github.com/Respect/Relational/blob/develop/tests/library/Respect/Relational/MapperTest.php#L291

alganet avatar Jan 12 '14 14:01 alganet

Hi @alganet I would be really honored to write the docs for this section. I'll check the tests for reference as suggested. It may take a little while (I've recently got a few extra work to do), but I'll definitely do it! :)

jonyhayama avatar Jan 13 '14 11:01 jonyhayama

Hi @alganet

So the problem of the styles we have already solved. The problem that I tried to report it is basically: It is not possible to save relational objects that have unique identifiers, even using a custom style. For in: Mapper\extractColumns it tries to identify the handle on the child's father. In the example you gave me works for both (father and son) have "ID" as an identifier.

cristopher-rodrigues avatar Jan 16 '14 12:01 cristopher-rodrigues