Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from spiral/save-tree-sequences
Browse files Browse the repository at this point in the history
save looped sequences + more tests
  • Loading branch information
wolfy-j authored Apr 25, 2017
2 parents 3b98194 + 89c8a73 commit 4d91118
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ protected function getRoles(): array
private function queueRelated(
ContextualCommandInterface $parentCommand
): ContextualCommandInterface {
//Command or command set needed to store
$innerCommand = $this->instance->queueStore(true);
/*
* Only queue parent relations when parent wasn't saved already, attention, this might
* cause an issues with very deep recursive structures (and it's expected).
*/
$innerCommand = $this->instance->queueStore(
!$this->instance->isLoaded()
);

if (!$this->isSynced($this->parent, $this->instance)) {
//Syncing FKs before primary command been executed
Expand Down
9 changes: 7 additions & 2 deletions source/Spiral/ORM/Entities/Relations/BelongsToRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ public function queueCommands(ContextualCommandInterface $parentCommand): Comman
private function queueRelated(
ContextualCommandInterface $parentCommand
): ContextualCommandInterface {
//Command or command set needed to store
$innerCommand = $this->instance->queueStore(true);
/*
* Only queue parent relations when parent wasn't saved already, attention, this might
* cause an issues with very deep recursive structures (and it's expected).
*/
$innerCommand = $this->instance->queueStore(
!$this->instance->isLoaded()
);

if (!$this->isSynced($this->parent, $this->instance)) {
//Syncing FKs before primary command been executed
Expand Down
7 changes: 7 additions & 0 deletions source/Spiral/ORM/RecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

interface RecordInterface extends EntityInterface
{
/**
* Indication that record has allocated database row.
*
* @return bool
*/
public function isLoaded(): bool;

/**
* Can be null.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/ORM/MySQL/TreeSequenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* orm
*
* @author Wolfy-J
*/

namespace Spiral\Tests\ORM\MySQL;

use Spiral\Tests\Database\MySQL\DriverTrait;

class TreeSequenceTest extends \Spiral\Tests\ORM\TreeSequenceTest
{
use DriverTrait;
}
15 changes: 15 additions & 0 deletions tests/ORM/Postgres/TreeSequenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* orm
*
* @author Wolfy-J
*/

namespace Spiral\Tests\ORM\Postgres;

use Spiral\Tests\Database\Postgres\DriverTrait;

class TreeSequenceTest extends \Spiral\Tests\ORM\TreeSequenceTest
{
use DriverTrait;
}
15 changes: 15 additions & 0 deletions tests/ORM/SQLServer/TreeSequenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* orm
*
* @author Wolfy-J
*/

namespace Spiral\Tests\ORM\SQLServer;

use Spiral\Tests\Database\SQLServer\DriverTrait;

class TreeSequenceTest extends \Spiral\Tests\ORM\TreeSequenceTest
{
use DriverTrait;
}
15 changes: 15 additions & 0 deletions tests/ORM/SQLite/TreeSequenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* orm
*
* @author Wolfy-J
*/

namespace Spiral\Tests\ORM\SQLite;

use Spiral\Tests\Database\SQLite\DriverTrait;

class TreeSequenceTest extends \Spiral\Tests\ORM\TreeSequenceTest
{
use DriverTrait;
}
103 changes: 103 additions & 0 deletions tests/ORM/TreeSequenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* orm
*
* @author Wolfy-J
*/

namespace Spiral\Tests\ORM;

use Spiral\ORM\Transaction;
use Spiral\Tests\ORM\Fixtures\Post;
use Spiral\Tests\ORM\Fixtures\User;

abstract class TreeSequenceTest extends BaseTest
{
public function testSaveParentAndChild()
{
$user = new User();

$post = new Post();
$post->author = $user;

$transaction = new Transaction();
$transaction->store($user);
$transaction->store($post);

$transaction->run();

$this->assertSameInDB($user);
$this->assertSameInDB($post);
}

public function testSaveChildAndParent()
{
$user = new User();

$post = new Post();
$post->author = $user;

$transaction = new Transaction();
$transaction->store($post);
$transaction->store($user);

$transaction->run();

$this->assertSameInDB($user);
$this->assertSameInDB($post);
}

public function testSaveChildAndParentLOOP()
{
$user = new User();

$post = new Post();
$post->author = $user;
$user->posts->add(clone $post);

$transaction = new Transaction();
$transaction->store($post);
$transaction->store($user);

$transaction->run();

$this->assertSameInDB($user);
$this->assertSameInDB($post);
}

public function testSaveChildAndParentLOOP_noParentSave()
{
$user = new User();

$post = new Post();
$post->author = $user;
$user->posts->add(clone $post);

$transaction = new Transaction();
$transaction->store($post);

$transaction->run();

$this->assertSameInDB($user);
$this->assertSameInDB($post);
}

public function testSavedParentAndChildLOOP()
{
$user = new User();

$post = new Post();
$post->author = $user;
$user->posts->add(clone $post);

$transaction = new Transaction();

$transaction->store($user);
$transaction->store($post);

$transaction->run();

$this->assertSameInDB($user);
$this->assertSameInDB($post);
}
}

0 comments on commit 4d91118

Please sign in to comment.