You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Double call Zenstruck\Foundry\Proxy->object() generates RuntimeException('Cannot auto refresh "Entity" as there are unsaved changes. Be sure to call ->save() or disable auto refreshing')
#564
I found an interesting bug with Zenstruck\Foundry\ModelFactory and Zenstruck\Foundry\Proxy.
Use:
zenstruck/foundry: "1.36.1"
"ramsey/uuid": "4.7.5",
"ramsey/uuid-doctrine": "2.0.0",
"doctrine/doctrine-bundle": "2.11.3",
"symfony/framework-bundle": "6.3.12"
Entity Quote and QuoteFactor
use Ramsey\Uuid\Doctrine\UuidV7Generator;
class Quote
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['event', 'quote'])]
private ?int $id = null;
#[ORM\Column(type: 'uuid', unique: true, nullable: false)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: UuidV7Generator::class)]
private ?string $uuid = null;
public function getUuid(): ?string
{
return $this->uuid;
}
public function setUuid(?string $uuid): void
{
$this->uuid = $uuid;
}
}
use Ramsey\Uuid\Uuid;
class QuoteFactory extends ModelFactory
{
protected function getDefaults(): array
{
return ['uuid' => Uuid::uuid7()->toString()];
}
protected static function getClass(): string
{
return Quote::class;
}
}
Unit test
class SomeTest extends BaseTestCase
{
public function testSomething(): void
{
$quote = QuoteFactory::createOne();
//to be sure that everything is up to date
$quote->save();
//First call - no exception
$quote->object();
//The second call calls the Exception
$quote->object();
/**
* class Zenstruck\Foundry\Proxy::130
* RuntimeException(Cannot auto refresh "Quote" as there are unsaved changes.
* Be sure to call ->save() or disable auto refreshing
* (see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#auto-refresh for details)
*/
}
}
Hello everyone.
I found an interesting bug with Zenstruck\Foundry\ModelFactory and Zenstruck\Foundry\Proxy.
Use:
Entity Quote and QuoteFactor
Unit test
Method
$this->objectManager()->getUnitOfWork()->computeChangeSet($om->getClassMetadata($this->class), $this->object)
RETURNS:I figured out why. Looks like after these steps the \RuntimeException appears:
FILE: https://github.com/zenstruck/foundry/blob/v1.36.1/src/Proxy.php
call Proxy->object()
Method "->object()" calls "->computeChangeSet()"(LINE 125) -> no changes -> CALL "$this->refresh()" (LINE 134);
Method "->refresh()" calls "$this->objectManager()->refresh($this->object);" (LINE 176)
Second call Proxy->object() calls "->computeChangeSet()"(LINE 125) -> some changes -> CALL "throw new \RuntimeException()" (LINE 130);
Looks like "refreshing entity" and calling "computeChangeSet" generates unsaved data and as a result exception.
Please take a look.
The text was updated successfully, but these errors were encountered: