foundry icon indicating copy to clipboard operation
foundry copied to clipboard

It seems like the foundry factories have a hardtime persisting entities with a composite primary key, when part of that composition is a foreign entity's id.

Open MauriceH1 opened this issue 1 year ago • 6 comments

edit: Added proper issue description and reproduction.

MauriceH1 avatar Nov 04 '25 01:11 MauriceH1

Please provide at least some code or even a reproducer

nikophil avatar Nov 04 '25 07:11 nikophil

Am gonna make a reproduction repo later today. 👍

MauriceH1 avatar Nov 04 '25 10:11 MauriceH1

@nikophil The link to the issue reproduction. https://github.com/MauriceH1/zenstruck-foundry-reproduction-1024

The issue crops up when you have 3 entities, in this case I've used: Company Employee Task

Company has a normal ID Employee has a composite ID composed of a uuid and the Company's ID Task has a composite ID composed of a uuid and the Employee's ID

The error you get is: In ORMInvalidArgumentException.php line 119:

Binding an entity with a composite primary key to a query is not supported. You should split the parameter into the explicit fields and bind them separately.

Reproduction steps:

$ docker compose run --rm backend composer install
$ docker compose run --rm backend bin/console doc:mig:mig
$ docker compose run --rm backend bin/console doc:fix:load

MauriceH1 avatar Nov 04 '25 13:11 MauriceH1

does this kind of mapping works in production code? (without Foundry)

I'm wondering if the problem is really related to Foundry? Or if Doctrine does not allow what you're trying to do?

nikophil avatar Nov 04 '25 16:11 nikophil

I got exactly the same error when I transform your code into this:

    public function load(ObjectManager $manager): void
    {
        $company = CompanyFactory::createOne([
            'businessName' => 'Github',
        ]);

        $employee = EmployeeFactory::createOne([
            'name' => 'John Doe',
            'company' => $company,
        ]);

        $task = TaskFactory::new([
            'employee' => $employee,
            'name' => 'Task 1',
        ])->withoutPersisting()->create();

        dump($task);
        $this->entityManager->persist($task);
        $this->entityManager->flush();
    }

notice the ->withoutPersisting() on TaskFactory. The task is actually created by Foundry, but still not persisted. And once persist() is called.... 💥

I do think the problem comes from the mapping + Doctrine, and not Foundry

I'm closing the issue, feel free to reopen it if you still think Foundry is the culprit 😅

nikophil avatar Nov 04 '25 17:11 nikophil

@nikophil just got the same issue yes! Am deciding to drop the composite key as it's overengineered anyway.

Apologies for the unnecessary issue!

I've used a similar setup in the past but dropped to plain SQL to persist the object, I guess that's why it worked.

MauriceH1 avatar Nov 04 '25 17:11 MauriceH1