Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transactions #51

Open
jonyhayama opened this issue Dec 20, 2013 · 7 comments
Open

Transactions #51

jonyhayama opened this issue Dec 20, 2013 · 7 comments

Comments

@jonyhayama
Copy link

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
Copy link
Author

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! :)

@williamespindola
Copy link
Member

Hello @jonathanhayama you can do yourself ;)

@cristopher-rodrigues
Copy link

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?

@alganet
Copy link
Member

alganet commented Jan 12, 2014

@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
Copy link
Member

alganet commented Jan 12, 2014

@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

@jonyhayama
Copy link
Author

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! :)

@cristopher-rodrigues
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants