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

Check 'alreadyInSave' before save transaction #1309

Merged
merged 1 commit into from
Dec 5, 2016
Merged

Check 'alreadyInSave' before save transaction #1309

merged 1 commit into from
Dec 5, 2016

Conversation

Xfaider48
Copy link
Contributor

@Xfaider48 Xfaider48 commented Dec 5, 2016

The proposed solution for #1310

@Xfaider48
Copy link
Contributor Author

Some examples

Scheme:

<table name="authors" namespace="Test" phpName="Author">
    <column name="id" type="integer" required="true" autoIncrement="true" primaryKey="true"/>
    <column name="first_name" type="varchar" size="255"/>
    <column name="last_name" type="varchar" size="255"/>
</table>

<table name="books" namespace="Test" phpName="Book">
    <column name="id" type="integer" required="true" autoIncrement="true" primaryKey="true"/>
    <column name="title" type="varchar" size="255"/>
    <column name="author_id" type="integer" required="true"/>
    <foreign-key foreignTable="authors">
        <reference local="author_id" foreign="id"/>
    </foreign-key>
</table>

Code in model 'Author'

class Author extends BaseAuthor
{
    public function preInsert(ConnectionInterface $con = null) {
        echo 'Author preInsert <br/>';
        return parent::preInsert($con);
    }

    public function preUpdate(ConnectionInterface $con = null) {
        echo 'Author preUpdate <br/>';
        return parent::preUpdate($con);
    }

    public function preSave(ConnectionInterface $con = null) {
        echo 'Author preSave <br/>';
        return parent::preSave($con);
    }

    public function postInsert(ConnectionInterface $con = null) {
        echo 'Author postInsert <br/>';
        parent::postInsert($con);
    }

    public function postUpdate(ConnectionInterface $con = null) {
        echo 'Author postUpdate <br/>';
        parent::postUpdate($con);
    }

    public function postSave(ConnectionInterface $con = null) {
        echo 'Author postSave <br/>';
        parent::postSave($con);
    }
}

Code in model 'Book'

class Book extends BaseBook
{
    public function preInsert(ConnectionInterface $con = null) {
        echo 'Book preInsert <br/>';
        return parent::preInsert($con);
    }

    public function preUpdate(ConnectionInterface $con = null) {
        echo 'Book preUpdate <br/>';
        return parent::preUpdate($con);
    }

    public function preSave(ConnectionInterface $con = null) {
        echo 'Book  preSave <br/>';
        return parent::preSave($con);
    }

    public function postInsert(ConnectionInterface $con = null) {
        echo 'Book postInsert <br/>';
        parent::postInsert($con);
    }

    public function postUpdate(ConnectionInterface $con = null) {
        echo 'Book postUpdate <br/>';
        parent::postUpdate($con);
    }

    public function postSave(ConnectionInterface $con = null) {
        echo 'Book postSave <br/>';
        parent::postSave($con);
    }
}

Tests without check 'alreadyInSave'

$author = new Author();
$author->setFirstName("Leo");
$author->setLastName("Tolstoy");

$book = new Book();
$book->setTitle("War & Peace");
$book->setAuthor($author);

If we call $author->save(); then we get the output:

Author preSave 
Author preInsert 
Book preSave 
Book preInsert 
Book postInsert 
Book postSave 
Author postInsert 
Author postSave 

And this is ok. But if we call $book->save(); we get this:

Book preSave 
Book preInsert 
Author preSave 
Author preInsert 
Book preSave 
Book preInsert 
Book postInsert 
Book postSave 
Author postInsert 
Author postSave 
Book postInsert 
Book postSave 

As you can see, Book hooks called twice. This happens, if we call save on related objects.

Tests with check 'alreadyInSave'

Results if we call $book->save(); like in section above:

Book preSave 
Book preInsert 
Author preSave 
Author preInsert 
Author postInsert 
Author postSave 
Book postInsert 
Book postSave

@SlyDeath
Copy link

SlyDeath commented Dec 5, 2016

+1

@marcj marcj merged commit 21c75aa into propelorm:master Dec 5, 2016
@marcj
Copy link
Member

marcj commented Dec 5, 2016

Good catch, thanks!

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

Successfully merging this pull request may close these issues.

4 participants