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 #7877 #7882

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 5 additions & 4 deletions lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,15 +680,16 @@ protected function prepareUpdateData($entity, bool $isInsert = false)
continue;
}

if ($newVal !== null) {
$oid = spl_object_id($newVal);
$oid = spl_object_id($newVal);

if (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) {
if (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal)) {
// Optimization: Self-referencing entities with application-provided IDs
// do not need extra updates
if (! ($newVal === $entity && $this->class->getIdentifierValues($newVal))) {
// The associated entity $newVal is not yet persisted, so we must
// set $newVal = null, in order to insert a null value and schedule an
// extra update on the UnitOfWork.
$uow->scheduleExtraUpdate($entity, [$field => [null, $newVal]]);

$newVal = null;
}
}
Expand Down
104 changes: 104 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/GH7877Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;

use function uniqid;

/**
* @group GH7877
*/
class GH7877Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->createSchemaForModels(
GH7877ApplicationGenerated::class,
GH7877DatabaseGenerated::class
);
}

public function testNotNullableColumnWorksWithApplicationGeneratedId(): void
{
$entity = new GH7877ApplicationGenerated($entityId = uniqid());
$entity->parent = $entity;
$this->_em->persist($entity);

$this->_em->flush();

$this->_em->clear();
$child = $this->_em->find(GH7877ApplicationGenerated::class, $entityId);
self::assertSame($entityId, $child->parent->id);
}

public function testNullableColumnWorksWithDatabaseGeneratedId(): void
{
$entity = new GH7877DatabaseGenerated();
$entity->parent = $entity;
$this->_em->persist($entity);

$this->_em->flush();

$entityId = $entity->id;
$this->_em->clear();

$child = $this->_em->find(GH7877DatabaseGenerated::class, $entityId);
self::assertSame($entityId, $child->parent->id);
}
}

/**
* @ORM\Entity
*/
class GH7877ApplicationGenerated
{
public function __construct(string $id)
{
$this->id = $id;
}

/**
* @ORM\Id
* @ORM\Column(type="string")
* @ORM\GeneratedValue(strategy="NONE")
*
* @var string
*/
public $id;

/**
* @ORM\ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\GH7877ApplicationGenerated")
* @ORM\JoinColumn(nullable=false)
*
* @var self
*/
public $parent;
}

/**
* @ORM\Entity
*/
class GH7877DatabaseGenerated
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var int
*/
public $id;

/**
* @ORM\ManyToOne(targetEntity="Doctrine\Tests\ORM\Functional\GH7877DatabaseGenerated")
*
* @var self
*/
public $parent;
}