Skip to content

Commit f91272d

Browse files
peldaxroxblnfkgithub-actions
authored
Optimize relations resolving (#491)
--------- Co-authored-by: roxblnfk <roxblnfk@ya.ru> Co-authored-by: github-actions <github-actions@users.noreply.github.com>
1 parent 2f3a9d7 commit f91272d

File tree

10 files changed

+195
-9
lines changed

10 files changed

+195
-9
lines changed

src/RelationMap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ private function registerOuterRelation(string $role, string $container, array $r
191191
return;
192192
}
193193

194+
if (isset($relationSchema[Relation::SCHEMA][Relation::INVERSION])) {
195+
// handshaked relation, skip
196+
return;
197+
}
198+
194199
$relation = new ShadowBelongsTo($container, $role, $relationSchema);
195200
$this->dependencies[$relation->getName()] = $relation;
196201
}

tests/ORM/Functional/Driver/Common/Integration/Case429/CaseTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,6 @@ private function makeTables(): void
117117
'order_item_id' => 'int,nullable',
118118
'quantity' => 'int',
119119
]);
120-
121-
$this->makeFK('order_item', 'order_id', 'order', 'id', 'NO ACTION', 'CASCADE');
122-
123-
$this->makeFK('order_item', 'purchase_order_id', 'purchase_order', 'id', 'NO ACTION', 'CASCADE');
124-
125-
$this->makeFK('purchase_order_item', 'purchase_order_id', 'purchase_order', 'id', 'NO ACTION', 'CASCADE');
126-
127-
$this->makeFK('purchase_order_item', 'order_item_id', 'order_item', 'id', 'NO ACTION', 'CASCADE');
128120
}
129121

130122
private function fillData(): void

tests/ORM/Functional/Driver/Common/Integration/Case429/schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
],
174174
Schema::SCOPE => null,
175175
Schema::TYPECAST => [
176-
'id' => 'primary',
176+
'id' => 'int',
177177
'purchase_order_id' => 'int',
178178
'order_item_id' => 'int',
179179
'quantity' => 'int',
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue489;
6+
7+
use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest;
8+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue489\Entity\User;
9+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\IntegrationTestTrait;
10+
use Cycle\ORM\Tests\Traits\TableTrait;
11+
12+
abstract class CaseTest extends BaseTest
13+
{
14+
use IntegrationTestTrait;
15+
use TableTrait;
16+
17+
public function testSave(): void
18+
{
19+
$this->captureWriteQueries();
20+
$this->save(new User());
21+
$this->assertNumWrites(1);
22+
}
23+
24+
public function setUp(): void
25+
{
26+
// Init DB
27+
parent::setUp();
28+
$this->makeTables();
29+
30+
$this->loadSchema(__DIR__ . '/schema.php');
31+
}
32+
33+
private function makeTables(): void
34+
{
35+
// Make tables
36+
$this->makeTable(User::ROLE, [
37+
'id' => 'primary', // autoincrement
38+
'user_id' => 'int,nullable',
39+
]);
40+
}
41+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue489\Entity;
6+
7+
class User
8+
{
9+
public const ROLE = 'user';
10+
11+
public ?int $id = null;
12+
public ?int $user_id = null;
13+
public ?self $user = null;
14+
public iterable $users = [];
15+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Cycle\ORM\Mapper\Mapper;
6+
use Cycle\ORM\Relation;
7+
use Cycle\ORM\Schema\GeneratedField;
8+
use Cycle\ORM\SchemaInterface as Schema;
9+
use Cycle\ORM\Select\Repository;
10+
use Cycle\ORM\Select\Source;
11+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue489\Entity\User;
12+
13+
return [
14+
'user' => [
15+
Schema::ENTITY => User::class,
16+
Schema::SOURCE => Source::class,
17+
Schema::MAPPER => Mapper::class,
18+
Schema::REPOSITORY => Repository::class,
19+
Schema::DATABASE => 'default',
20+
Schema::TABLE => 'user',
21+
Schema::PRIMARY_KEY => ['id'],
22+
Schema::FIND_BY_KEYS => ['id'],
23+
Schema::COLUMNS => [
24+
'id' => 'id',
25+
'user_id' => 'user_id',
26+
],
27+
Schema::RELATIONS => [
28+
'user' => [
29+
Relation::TYPE => Relation::BELONGS_TO,
30+
Relation::TARGET => 'user',
31+
Relation::LOAD => Relation::LOAD_PROMISE,
32+
Relation::SCHEMA => [
33+
Relation::CASCADE => true,
34+
Relation::NULLABLE => true,
35+
Relation::INNER_KEY => 'user_id',
36+
Relation::OUTER_KEY => ['id'],
37+
// Relation::INVERSION => 'users',
38+
],
39+
],
40+
'users' => [
41+
Relation::TYPE => Relation::HAS_MANY,
42+
Relation::TARGET => 'user',
43+
Relation::LOAD => Relation::LOAD_PROMISE,
44+
Relation::SCHEMA => [
45+
Relation::CASCADE => true,
46+
Relation::NULLABLE => false, // The reason for the issue
47+
Relation::WHERE => [],
48+
Relation::ORDER_BY => [],
49+
Relation::INNER_KEY => ['id'],
50+
Relation::OUTER_KEY => 'user_id',
51+
// Relation::INVERSION => 'user',
52+
],
53+
],
54+
],
55+
Schema::SCOPE => null,
56+
Schema::TYPECAST => [
57+
'id' => 'int',
58+
'user_id' => 'int',
59+
],
60+
Schema::SCHEMA => [],
61+
Schema::GENERATED_FIELDS => [
62+
'id' => GeneratedField::ON_INSERT,
63+
],
64+
],
65+
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\MySQL\Integration\Issue489;
6+
7+
// phpcs:ignore
8+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue489\CaseTest as CommonClass;
9+
10+
/**
11+
* @group driver
12+
* @group driver-mysql
13+
*/
14+
class CaseTest extends CommonClass
15+
{
16+
public const DRIVER = 'mysql';
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\Postgres\Integration\Issue489;
6+
7+
// phpcs:ignore
8+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue489\CaseTest as CommonClass;
9+
10+
/**
11+
* @group driver
12+
* @group driver-postgres
13+
*/
14+
class CaseTest extends CommonClass
15+
{
16+
public const DRIVER = 'postgres';
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\SQLServer\Integration\Issue489;
6+
7+
// phpcs:ignore
8+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue489\CaseTest as CommonClass;
9+
10+
/**
11+
* @group driver
12+
* @group driver-sqlserver
13+
*/
14+
class CaseTest extends CommonClass
15+
{
16+
public const DRIVER = 'sqlserver';
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Tests\Functional\Driver\SQLite\Integration\Issue489;
6+
7+
// phpcs:ignore
8+
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue489\CaseTest as CommonClass;
9+
10+
/**
11+
* @group driver
12+
* @group driver-sqlite
13+
*/
14+
class CaseTest extends CommonClass
15+
{
16+
public const DRIVER = 'sqlite';
17+
}

0 commit comments

Comments
 (0)