Doctrine Common
  1. Doctrine Common
  2. DCOM-40

Using shared references with the SharedFixtureInterface with the DataFixtures extension will not work.

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.1.2
    • Component/s: None
    • Labels:
      None

      Description

      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)

      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

      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.

        Activity

        Hide
        Steven Rosato added a comment -

        Added patch that addresses the issue.

        Show
        Steven Rosato added a comment - Added patch that addresses the issue.
        Hide
        Guilherme Blanco added a comment -

        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.

        Show
        Guilherme Blanco added a comment - 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.

          People

          • Assignee:
            Guilherme Blanco
            Reporter:
            Steven Rosato
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: