Skip to content

Loading…

DCOM-40: Using shared references with the SharedFixtureInterface with the DataFixtures extension will not work. #640

Closed
doctrinebot opened this Issue · 3 comments

2 participants

@doctrinebot

Jira issue originally created by user ratius:

I did not know where to issue that component, but since this extension using the Doctrine Common namespace I thought it relevant to issue the bug here.

Consider the following shared fixures (as stated on jwage's github repo here: https://github.com/doctrine/data-fixtures)

{quote}
In case if fixture objects have relations to other fixtures, it is now possible to easily add a reference to that object by name and later reference it to form a relation. Here is an example fixtures for Role and User relation
{quote}

namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;

class LoadUserRoleData extends AbstractFixture
{
    public function load($manager)
    {
        $adminRole = new Role();
        $adminRole->setName('admin');

        $anonymousRole = new Role;
        $anonymousRole->setName('anonymous');

        $manager->persist($adminRole);
        $manager->persist($anonymousRole);
        $manager->flush();

        // store reference to admin role for User relation to Role
        $this->addReference('admin-role', $adminRole);
    }
}

namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;

class LoadUserData extends AbstractFixture
{
    public function load($manager)
    {
        $user = new User();
        $user->setUsername('jwage');
        $user->setPassword('test');
        $user->setRole(
            $this->getReference('admin-role') // load the stored reference
        );

        $manager->persist($user);
        $manager->flush();

        // store reference of admin-user for other Fixtures
        $this->addReference('admin-user', $user);
    }
}

This will not use the last reference as a MANAGED entity but whether as a NEW one since the manager gets cleared (thus the unit of work) on each call to load() for the AbstractExecutor and thus marking any new references to the 'admin-user' considered a NEW entity, which should not be the case.

The current workaround is to directly fetch the entity using the EM's find() function, but that completely eliminates the main goal SharedFixtures' references are bringing.

I have provided the small patch that adress this issue, tests still pass.

@doctrinebot

Comment created by ratius:

Added patch that addresses the issue.

@doctrinebot

Comment created by @guilhermeblanco:

Not related to Common package (do we have a project for that?), but it seems this issue was already addressed when I looked at data-fixture repository.
https://github.com/doctrine/data-fixtures/blob/master/lib/Doctrine/Common/DataFixtures/Executor/AbstractExecutor.php#L79

Closing as fixed.

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot doctrinebot added the Bug label
@doctrinebot doctrinebot added this to the 2.1.2 milestone
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.