Comments (4)
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.
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.
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.
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)
- Doctrine embeddables with v2 HOT 8
- [Tests] use 2 schemas in the same database
- Migration Guide to version 2 HOT 3
- V2 and phpstan HOT 5
- Upgrade from 1.37 to 1.38 introduce a memory leak HOT 16
- H
- [2.x] ArgumentCountError thrown when passing $attributes array to closure in event HOT 9
- Creation of dynamic property App\Entity\MyEntity::$_autoRefresh is deprecated HOT 6
- Issue with Proxy Entities in Version 2 HOT 14
- Zenstruck\Foundry\Factory::$class must not be accessed before initialization HOT 6
- Find functions do not refresh entity HOT 2
- Attribute wrapped in a Foundry Proxy is not compatible with `Symfony\Component\VarExporter\LazyProxyTrait` HOT 6
- Foundry should not remove symfony-s errorHandlers HOT 6
- Global state relationships and flush_after HOT 8
- How to fix deprecation notices HOT 7
- [2.0] Problem with inversed `OneToOne` relationships HOT 4
- [2.0] No autocompletion for Proxy objects HOT 2
- [2.0] ResetDatabase with DamaDoctrineTestBundle and symfony/doctrine-messenger library
- [2.0] Re-add `assertPersisted()` & `assertNotPersisted()` HOT 4
- [2.0] Customize Faker's locale HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from foundry.