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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
wolfy-j committed Apr 25, 2017
2 parents 81c4216 + 7acdf0b commit e6b7125
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 8 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
CHANGELOG for 0.9.0 RC
======================

0.9.9 (28.03.2017)
1.0.1 (25.04.2017)
-----
- now possible to use 'limit' and 'orderBy' options in HasMany and ManyToMany loaders
- new Record constant 'ORDER_BY', defines default order for related data (exclude: with-relations)
- BelongsTo and BelongsToMorphed relations do not save parent relations when parent is already loaded (to prevent recursion loops)

0.9.7 (15.03.2017)
0.9.7 (15.03.2017) = 1.0.0 public release
-----
- fetchAll
- better phpDoc
Expand Down
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
5 changes: 5 additions & 0 deletions source/Spiral/ORM/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public function run(bool $forceTransaction = false, bool $clean = true)
$commands[] = $command;
}

if (empty($commands)) {
return;
}

//Commands we executed and drivers with started transactions
$executedCommands = $wrappedDrivers = [];

Expand Down Expand Up @@ -138,6 +142,7 @@ public function run(bool $forceTransaction = false, bool $clean = true)
}

foreach (array_reverse($wrappedDrivers) as $driver) {
/** @var Driver $driver */
$driver->commitTransaction();
}

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 e6b7125

Please sign in to comment.