-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #432: Fix belongs to relation when parent is chang…
…ed using parent id Fixed expected behavior: If we change parent ID in a BelongsTo relation and store the child entity, the parent entity will be changed in the relation with the new one. Before it was like this: the nullable BelongsTo relation can't be finished if the parent had loaded state.
- Loading branch information
Showing
15 changed files
with
376 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
tests/ORM/Functional/Driver/Common/Integration/Case346/CaseTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case346; | ||
|
||
use Cycle\ORM\Select; | ||
use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest; | ||
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\IntegrationTestTrait; | ||
use Cycle\ORM\Tests\Traits\TableTrait; | ||
|
||
abstract class CaseTest extends BaseTest | ||
{ | ||
use IntegrationTestTrait; | ||
use TableTrait; | ||
|
||
public function setUp(): void | ||
{ | ||
// Init DB | ||
parent::setUp(); | ||
$this->makeTables(); | ||
$this->fillData(); | ||
|
||
$this->loadSchema(__DIR__ . '/schema.php'); | ||
} | ||
|
||
public function testSelect(): void | ||
{ | ||
/** @var Entity\Post $post */ | ||
$post = (new Select($this->orm, Entity\Post::class)) | ||
->wherePK(1) | ||
->fetchOne(); | ||
|
||
$this->assertSame(1, $post->user->id); | ||
|
||
$post->user_id = 2; | ||
|
||
$this->save($post); | ||
|
||
$this->assertSame(2, $post->user_id); | ||
$this->assertSame(2, $post->user->id); | ||
$this->orm->getHeap()->clean(); | ||
} | ||
|
||
private function makeTables(): void | ||
{ | ||
// Make tables | ||
$this->makeTable(Entity\User::ROLE, [ | ||
'id' => 'primary', // autoincrement | ||
'login' => 'string', | ||
'created_at' => 'datetime', | ||
'updated_at' => 'datetime', | ||
]); | ||
|
||
$this->makeTable('post', [ | ||
'id' => 'primary', | ||
'user_id' => 'int', | ||
'slug' => 'string', | ||
'title' => 'string', | ||
'public' => 'bool', | ||
'content' => 'string', | ||
'published_at' => 'datetime,nullable', | ||
'created_at' => 'datetime', | ||
'updated_at' => 'datetime', | ||
'deleted_at' => 'datetime,nullable', | ||
]); | ||
$this->makeFK('post', 'user_id', 'user', 'id', 'NO ACTION', 'NO ACTION'); | ||
} | ||
|
||
private function fillData(): void | ||
{ | ||
$this->getDatabase()->table('user')->insertMultiple( | ||
['login'], | ||
[ | ||
['user-1'], | ||
['user-2'], | ||
['user-3'], | ||
['user-4'], | ||
], | ||
); | ||
$this->getDatabase()->table('post')->insertMultiple( | ||
['user_id', 'slug', 'title', 'public', 'content'], | ||
[ | ||
[1, 'slug-string-1', 'Title 1', true, 'Foo-bar-baz content 1'], | ||
[2, 'slug-string-2', 'Title 2', true, 'Foo-bar-baz content 2'], | ||
[2, 'slug-string-3', 'Title 3', true, 'Foo-bar-baz content 3'], | ||
[3, 'slug-string-4', 'Title 4', true, 'Foo-bar-baz content 4'], | ||
[3, 'slug-string-5', 'Title 5', true, 'Foo-bar-baz content 5'], | ||
[3, 'slug-string-6', 'Title 6', true, 'Foo-bar-baz content 6'], | ||
], | ||
); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/ORM/Functional/Driver/Common/Integration/Case346/Entity/Post.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case346\Entity; | ||
|
||
use DateTimeImmutable; | ||
|
||
class Post | ||
{ | ||
public ?int $id = null; | ||
public string $slug; | ||
public string $title = ''; | ||
public bool $public = false; | ||
public string $content = ''; | ||
public DateTimeImmutable $created_at; | ||
public DateTimeImmutable $updated_at; | ||
public ?DateTimeImmutable $published_at = null; | ||
public ?DateTimeImmutable $deleted_at = null; | ||
public User $user; | ||
public ?int $user_id = null; | ||
|
||
public function __construct(string $title = '', string $content = '') | ||
{ | ||
$this->title = $title; | ||
$this->content = $content; | ||
$this->created_at = new DateTimeImmutable(); | ||
$this->updated_at = new DateTimeImmutable(); | ||
$this->resetSlug(); | ||
} | ||
|
||
public function resetSlug(): void | ||
{ | ||
$this->slug = \bin2hex(\random_bytes(32)); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/ORM/Functional/Driver/Common/Integration/Case346/Entity/User.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case346\Entity; | ||
|
||
use DateTimeImmutable; | ||
|
||
class User | ||
{ | ||
public const ROLE = 'user'; | ||
|
||
public ?int $id = null; | ||
public string $login; | ||
public DateTimeImmutable $created_at; | ||
public DateTimeImmutable $updated_at; | ||
/** @var iterable<Post> */ | ||
public iterable $posts = []; | ||
|
||
public function __construct(string $login, string $password) | ||
{ | ||
$this->login = $login; | ||
$this->created_at = new DateTimeImmutable(); | ||
$this->updated_at = new DateTimeImmutable(); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
tests/ORM/Functional/Driver/Common/Integration/Case346/schema.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Cycle\ORM\Mapper\Mapper; | ||
use Cycle\ORM\Relation; | ||
use Cycle\ORM\SchemaInterface as Schema; | ||
use Cycle\ORM\Select\Source; | ||
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case346\Entity\Post; | ||
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case346\Entity\User; | ||
|
||
return [ | ||
'post' => [ | ||
Schema::ENTITY => Post::class, | ||
Schema::SOURCE => Source::class, | ||
Schema::DATABASE => 'default', | ||
Schema::MAPPER => Mapper::class, | ||
Schema::TABLE => 'post', | ||
Schema::PRIMARY_KEY => ['id'], | ||
Schema::FIND_BY_KEYS => ['id'], | ||
Schema::COLUMNS => [ | ||
'id' => 'id', | ||
'slug' => 'slug', | ||
'title' => 'title', | ||
'public' => 'public', | ||
'content' => 'content', | ||
'created_at' => 'created_at', | ||
'updated_at' => 'updated_at', | ||
'published_at' => 'published_at', | ||
'deleted_at' => 'deleted_at', | ||
'user_id' => 'user_id', | ||
], | ||
Schema::RELATIONS => [ | ||
'user' => [ | ||
Relation::TYPE => Relation::BELONGS_TO, | ||
Relation::TARGET => User::ROLE, | ||
Relation::LOAD => Relation::LOAD_PROMISE, | ||
Relation::SCHEMA => [ | ||
Relation::CASCADE => true, | ||
Relation::NULLABLE => true, | ||
Relation::INNER_KEY => 'user_id', | ||
Relation::OUTER_KEY => ['id'], | ||
], | ||
], | ||
], | ||
Schema::TYPECAST => [ | ||
'id' => 'int', | ||
'public' => 'bool', | ||
'created_at' => 'datetime', | ||
'updated_at' => 'datetime', | ||
'published_at' => 'datetime', | ||
'deleted_at' => 'datetime', | ||
'user_id' => 'int', | ||
], | ||
Schema::SCHEMA => [], | ||
], | ||
'user' => [ | ||
Schema::ENTITY => User::class, | ||
Schema::MAPPER => Mapper::class, | ||
Schema::SOURCE => Source::class, | ||
Schema::DATABASE => 'default', | ||
Schema::TABLE => 'user', | ||
Schema::PRIMARY_KEY => ['id'], | ||
Schema::FIND_BY_KEYS => ['id'], | ||
Schema::COLUMNS => [ | ||
'id' => 'id', | ||
'login' => 'login', | ||
'created_at' => 'created_at', | ||
'updated_at' => 'updated_at', | ||
], | ||
Schema::RELATIONS => [ | ||
// 'posts' => [ | ||
// Relation::TYPE => Relation::HAS_MANY, | ||
// Relation::TARGET => 'post', | ||
// Relation::COLLECTION_TYPE => 'array', | ||
// Relation::LOAD => Relation::LOAD_PROMISE, | ||
// Relation::SCHEMA => [ | ||
// Relation::CASCADE => true, | ||
// Relation::NULLABLE => false, | ||
// Relation::WHERE => [], | ||
// Relation::ORDER_BY => [], | ||
// Relation::INNER_KEY => ['id'], | ||
// Relation::OUTER_KEY => 'user_id', | ||
// ], | ||
// ], | ||
], | ||
Schema::SCOPE => null, | ||
Schema::TYPECAST => [ | ||
'id' => 'int', | ||
'created_at' => 'datetime', | ||
'updated_at' => 'datetime', | ||
], | ||
Schema::SCHEMA => [], | ||
], | ||
]; |
Oops, something went wrong.