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

Using bigint as datatype causes "unsaved changes" error. #756

Closed
KDederichs opened this issue Dec 17, 2024 · 6 comments
Closed

Using bigint as datatype causes "unsaved changes" error. #756

KDederichs opened this issue Dec 17, 2024 · 6 comments
Labels
bug Something isn't working proxy

Comments

@KDederichs
Copy link
Contributor

Heya got another fun one for you (though I'm not sure if it's not a Doctrine bug) :)

Given a class Bar:

class Bar
{
    #[Id, Column(type: UuidType::NAME)]
    private Uuid $id;
    #[Column(type: 'bigint', nullable: false, options: ['default' => "0"])]
    private ?string $baz = '0';

    public function getId(): Uuid
    {
        return $this->id;
    }

    public function __construct()
    {
        $this->id = Uuid::v7();
    }

    public function getBaz(): ?string
    {
        return $this->baz;
    }

    public function setBaz(?string $baz): self
    {
        $this->baz = $baz;

        return $this;
    }
}

and a test like this:

    public function testGetBar(): void
    {
        $bar = BarFactory::createOne();
        self::assertEquals($bar->getId(), $bar->getId());
    }

you'll get this error:

Zenstruck\Foundry\Persistence\Exception\RefreshObjectFailed: Cannot auto refresh "App\Entity\Bar" 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).

Looking at the changeset for doctrine it'll for some reason try to convert 0 to "0" even though at no point in time that's accessed as integer.

That also seems to work as expected in DBAL 3 but errors out like this in DBAL 4.

Like I said not sure if it's a foundry thing or not, if it's not tell me and I'll toss it to the doctrine folks :)

@nikophil
Copy link
Member

hum, I'm not very familiar with bigints 🤔 what does your factory look like?

@KDederichs
Copy link
Contributor Author

<?php

namespace App\Factory;

use App\Entity\Bar;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;

/**
 * @extends PersistentProxyObjectFactory<Bar>
 */
final class BarFactory extends PersistentProxyObjectFactory
{
    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
     *
     * @todo inject services if required
     */
    public function __construct()
    {
    }

    public static function class(): string
    {
        return Bar::class;
    }

    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
     *
     * @todo add your default values here
     */
    protected function defaults(): array|callable
    {
        return [

        ];
    }

    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
     */
    protected function initialize(): static
    {
        return $this
            // ->afterInstantiate(function(Bar $bar): void {})
        ;
    }
}

Empty, it takes the default '0' when creating the object.

@nikophil nikophil added bug Something isn't working proxy labels Dec 17, 2024
@nikophil
Copy link
Member

sorry, but I can't reproduce the problem. Even with doctrine/orm ^2 it is working 🤔

Could you create a public reproducer? or even better: a failing test case in Foundry's tests

@KDederichs
Copy link
Contributor Author

https://github.com/KDederichs/sf-reproducer/tree/foundy/unsaved_changes here's a normal reproducer, (it's a DBAL4 thing so ORM 3 I guess, it doesn't happen with DBAL3).

I don't think I can just mess with the foundry test dependencies to force DBAL4 somehow?

@nikophil
Copy link
Member

I don't think I can just mess with the foundry test dependencies to force DBAL4 somehow?

Foundry does not add a constraint on the DBAL, it is compatible with orm 2/3 and dbal 3/4

@nikophil
Copy link
Member

nikophil commented Jan 7, 2025

For the record: doctrine/dbal#6650

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working proxy
Development

No branches or pull requests

2 participants