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

Fix loading for Embedded entities when parent is null #423

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/Parser/AbstractNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ public function parseRow(int $offset, array $row): int
{
$data = $this->fetchData($offset, $row);

$innerOffset = 0;
$relatedNodes = \array_merge(
$this->mergeParent === null ? [] : [$this->mergeParent],
$this->nodes,
$this->mergeSubclass
);

if ($this->isEmptyPrimaryKey($data)) {
// Skip all columns which are related to current node and sub nodes.
return \count($this->columns)
+ \array_reduce(
$relatedNodes,
static fn (int $cnt, AbstractNode $node): int => $cnt + \count($node->columns),
0,
);
}

if ($this->deduplicate($data)) {
foreach ($this->indexedData->getIndexes() as $index) {
try {
Expand All @@ -118,13 +135,7 @@ public function parseRow(int $offset, array $row): int
$this->push($data);
}

$innerOffset = 0;
$iterate = array_merge(
$this->mergeParent === null ? [] : [$this->mergeParent],
$this->nodes,
$this->mergeSubclass
);
foreach ($iterate as $node) {
foreach ($relatedNodes as $node) {
if (!$node->joined) {
continue;
}
Expand Down Expand Up @@ -379,4 +390,19 @@ protected function intersectData(array $keys, array $data): array
}
return $result;
}

/**
* @param array<string, mixed> $data
*
* @return bool True if any PK field is empty
*/
private function isEmptyPrimaryKey(array $data): bool
{
foreach ($this->duplicateCriteria as $key) {
if ($data[$key] === null) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422;

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\Functional\Driver\Common\Integration\Issue422\Entity\Billing;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\Entity\User;
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 User $user */
$user = (new Select($this->orm, Entity\User::class))
->load('billing')
->wherePK(1)
->fetchOne();
$this->assertInstanceOf(Billing::class, $user->billing);

/** @var User $user */
$user = (new Select($this->orm, Entity\User::class))
->load('billing')
->wherePK(2)
->fetchOne();
$this->assertNull($user->billing);
$this->assertSame(200, $user->otherEmbedded->propertyInt);
}

private function makeTables(): void
{
// Make tables
$this->makeTable('user', [
'id' => 'primary',
'name' => 'string',
'property_string' => 'string',
'property_int' => 'int',
]);

$this->makeTable('billing', [
'id' => 'primary',
'user_id' => 'int',
'property_string' => 'string',
'property_int' => 'int',
]);
$this->makeFK('billing', 'user_id', 'user', 'id', 'NO ACTION', 'NO ACTION');
}

private function fillData(): void
{
$this->getDatabase()->table('user')->insertMultiple(
['name', 'property_string', 'property_int'],
[
['user-with-billing', 'foo', 100],
['user-without-billing', 'bar', 200],
],
);
$this->getDatabase()->table('billing')->insertMultiple(
['user_id', 'property_string', 'property_int'],
[
[1, 'foo', 100],
],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\Entity;

class Billing
{
public ?int $id = null;

public SomeEmbedded $someEmbedded;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\Entity;

class SomeEmbedded
{
public string $propertyString = '';

public int $propertyInt = 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\Entity;

class User
{
public ?int $id = null;
public ?Billing $billing = null;
public SomeEmbedded $otherEmbedded;
}
123 changes: 123 additions & 0 deletions tests/ORM/Functional/Driver/Common/Integration/Issue422/schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types=1);

use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\Relation;
use Cycle\ORM\SchemaInterface as Schema;
use Cycle\ORM\Select\Repository;
use Cycle\ORM\Select\Source;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\Entity\Billing;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\Entity\User;
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\Entity\SomeEmbedded;

return [
'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',
],
Schema::RELATIONS => [
'billing' => [
Relation::TYPE => Relation::HAS_ONE,
Relation::TARGET => 'billing',
Relation::LOAD => Relation::LOAD_PROMISE,
Relation::SCHEMA => [
Relation::CASCADE => true,
Relation::NULLABLE => true,
Relation::INNER_KEY => ['id'],
Relation::OUTER_KEY => 'user_id',
],
],
'otherEmbedded' => [
Relation::TYPE => Relation::EMBEDDED,
Relation::TARGET => 'user:otherEmbeddable:otherEmbeddable',
Relation::LOAD => Relation::LOAD_EAGER,
Relation::SCHEMA => [],
],
],
Schema::SCOPE => null,
Schema::TYPECAST => [
'id' => 'int',
],
Schema::SCHEMA => [],
],
'billing' => [
Schema::ENTITY => Billing::class,
Schema::MAPPER => Cycle\ORM\Mapper\Mapper::class,
Schema::SOURCE => Cycle\ORM\Select\Source::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'billing',
Schema::PRIMARY_KEY => ['id'],
Schema::FIND_BY_KEYS => ['id'],
Schema::COLUMNS => [
'id' => 'id',
'user_id' => 'user_id',
],
Schema::RELATIONS => [
'someEmbeddable' => [
Relation::TYPE => Relation::EMBEDDED,
Relation::TARGET => 'billing:someEmbeddable:someEmbeddable',
Relation::LOAD => Relation::LOAD_EAGER,
Relation::SCHEMA => [],
],
],
Schema::SCOPE => null,
Schema::TYPECAST => [
'id' => 'int',
],
Schema::SCHEMA => [],
],
'billing:someEmbeddable:someEmbeddable' => [
Schema::ENTITY => SomeEmbedded::class,
Schema::MAPPER => Mapper::class,
Schema::SOURCE => Source::class,
Schema::REPOSITORY => Repository::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'billing',
Schema::PRIMARY_KEY => ['id'],
Schema::FIND_BY_KEYS => ['id'],
Schema::COLUMNS => [
'propertyString' => 'property_string',
'propertyInt' => 'property_int',
'id' => 'id',
],
Schema::RELATIONS => [],
Schema::SCOPE => null,
Schema::TYPECAST => [
'propertyString' => 'string',
'propertyInt' => 'int',
'id' => 'int',
],
Schema::SCHEMA => [],
],
'user:otherEmbeddable:otherEmbeddable' => [
Schema::ENTITY => SomeEmbedded::class,
Schema::MAPPER => Mapper::class,
Schema::SOURCE => Source::class,
Schema::REPOSITORY => Repository::class,
Schema::DATABASE => 'default',
Schema::TABLE => 'user',
Schema::PRIMARY_KEY => ['id'],
Schema::FIND_BY_KEYS => ['id'],
Schema::COLUMNS => [
'propertyString' => 'property_string',
'propertyInt' => 'property_int',
'id' => 'id',
],
Schema::RELATIONS => [],
Schema::SCOPE => null,
Schema::TYPECAST => [
'propertyString' => 'string',
'propertyInt' => 'int',
'id' => 'int',
],
Schema::SCHEMA => [],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\MySQL\Integration\Issue422;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\CaseTest as CommonClass;

/**
* @group driver
* @group driver-mysql
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'mysql';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\Postgres\Integration\Issue422;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\CaseTest as CommonClass;

/**
* @group driver
* @group driver-postgres
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'postgres';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\SQLServer\Integration\Issue422;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\CaseTest as CommonClass;

/**
* @group driver
* @group driver-sqlserver
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'sqlserver';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Cycle\ORM\Tests\Functional\Driver\SQLite\Integration\Issue422;

// phpcs:ignore
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\Issue422\CaseTest as CommonClass;

/**
* @group driver
* @group driver-sqlite
*/
class CaseTest extends CommonClass
{
public const DRIVER = 'sqlite';
}
Loading