Skip to content

Commit

Permalink
fix: add unpersisted object to relation
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Jan 6, 2025
1 parent 3e9650a commit d644104
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Persistence/PersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public function refresh(object &$object, bool $force = false): object

public function isPersisted(object $object): bool
{
// prevents doctrine to use its cache and think the object is persisted
if ($this->strategyFor($object::class)->isScheduledForInsert($object)) {
return false;
}
Expand Down
21 changes: 19 additions & 2 deletions src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,29 @@ protected function normalizeObject(object $object): object

$configuration = Configuration::instance();

if (!$configuration->isPersistenceAvailable() || !$configuration->persistence()->hasPersistenceFor($object)) {
if (!$configuration->isPersistenceAvailable()) {
return $object;
}

$persistenceManager = $configuration->persistence();

if ($object instanceof Proxy) {
$proxy = $object;
$proxy->_disableAutoRefresh();
$object = $proxy->_real();
$proxy->_enableAutoRefresh();
}

if (!$persistenceManager->hasPersistenceFor($object)) {
return $object;
}

if (!$persistenceManager->isPersisted($object)) {
$persistenceManager->scheduleForInsert($object);
}

try {
return $configuration->persistence()->refresh($object, true);
return $persistenceManager->refresh($object, force: true);
} catch (RefreshObjectFailed|VarExportLogicException) {
return $object;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Zenstruck\Foundry\Tests\Fixture\Entity\Contact;
use Zenstruck\Foundry\Tests\Fixture\Entity\Tag;

use function Zenstruck\Foundry\Persistence\refresh;
use function Zenstruck\Foundry\Persistence\unproxy;

/**
Expand Down Expand Up @@ -361,6 +362,43 @@ public function ensure_one_to_many_relations_are_not_pre_persisted(): void
}
}

/** @test */
#[Test]
#[DataProvider('provideCascadeRelationshipsCombinations')]
#[UsingRelationships(Category::class, ['contacts'])]
public function it_can_add_managed_entity_to_many_to_one(): void
{
$this->it_can_add_entity_to_many_to_one(
static::categoryFactory()->create()
);
}

/** @test */
#[Test]
#[DataProvider('provideCascadeRelationshipsCombinations')]
#[UsingRelationships(Category::class, ['contacts'])]
public function it_can_add_unmanaged_entity_to_many_to_one(): void
{
$this->it_can_add_entity_to_many_to_one(
static::categoryFactory()->withoutPersisting()->create()
);
}

private function it_can_add_entity_to_many_to_one(Category $category): void
{
self::assertCount(0, $category->getContacts());

$contact1 = static::contactFactory()->create(['category' => $category]);
$contact2 = static::contactFactory()->create(['category' => $category]);

static::categoryFactory()::assert()->count(1);

self::assertCount(2, $category->getContacts());

self::assertSame(unproxy($category), $contact1->getCategory());
self::assertSame(unproxy($category), $contact2->getCategory());
}

/** @return PersistentObjectFactory<Contact> */
protected static function contactFactoryWithoutCategory(): PersistentObjectFactory
{
Expand Down

0 comments on commit d644104

Please sign in to comment.