Git Product home page Git Product logo

Comments (4)

nikophil avatar nikophil commented on July 4, 2024 2

this is due to the GeneratedValue, when the object is created, doctrine will update the value of your $id, disregarding whether it was filled-in or not.

A solution would be to not use an auto-generated value, and set the id in the constructor;

// Profile.php

    #[ORM\Id]
    #[ORM\Column(type: UlidType::NAME, unique: true)]
    private Ulid $id;

    public function __construct(?Ulid $id = null) 
    {
        $this->id = $id ?? new Ulid::generate(now());
    }

I think this solution is always preferable to the auto-generated one, because you always have a valid object (ie: Profile::$id should never be null, and is never null with this solution). Another good point for this solution is that you can access the id before the object is stored in db which could be useful in some rare occasions.

But because you're using a trait for your ids, you may won't like this solution, so you'll have to use forceSet() + save() I guess

from foundry.

nikophil avatar nikophil commented on July 4, 2024

Hello @orangevinz

seems legit to me:

$profile = ProfileFactory::createOne(['name' => 'John Doe']); // $profile is a `Proxy<Profile>` and is already stored in database
$profile->forceSet('id', Ulid::fromString(self::PROFILE_JOHN_DOE)); // id is updated in the variable, but not in db
$profile->save(); // update id in db

The weird behavior comes from the fact that createMany() triggers a flush in doctrine, so if it occurs after the "static profile" creation, the id gets finally updated.

by the way, you can enable "always force properties" in order to not bother with this kind of things, and directly write:

ProfileFactory::createOne(['name' => 'John Doe', 'id' => Ulid::fromString(self::PROFILE_JOHN_DOE));

from foundry.

orangevinz avatar orangevinz commented on July 4, 2024

Hey @nikophil
Thanks a lot for the clarification. Unfortunately I still can't force the id this way.
I can't figure out what I missed

final class ProfileFactory extends ModelFactory
{
    protected function initialize(): self
    {
        return $this->instantiateWith(
            (new Instantiator())
                ->withoutConstructor()
                ->alwaysForceProperties(['id'])
        );
    }
final class ProfileStory extends Story
{
    public const PROFILE_JOHN_DOE = '018f4de7-511a-6249-4064-e2950b4f6f14';

    public function build(): void
    {
        // And a static profile
        ProfileFactory::createOne([
            'id' => Ulid::fromString(self::PROFILE_JOHN_DOE),
            'name' => 'John Doe',
        ]);

        // Create x dynamic profiles
        ProfileFactory::createMany(20);
    }
}
namespace App\Entity\Traits;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Component\Uid\Ulid;

trait UlidIdentifierEntity
{
    #[ORM\Id]
    #[ORM\Column(type: UlidType::NAME, unique: true)]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: UlidGenerator::class)]
    private ?Ulid $id;

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

from foundry.

orangevinz avatar orangevinz commented on July 4, 2024

Ok it's very clear, I'll stick with forceSet and save then, unless I manage to stop needing a static id 😉
Thanks again for your help and for this library.

from foundry.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.