Symfony2功能测试+原则fixture:在原则fixture中刷新后,对实体的更改不会保存


Symfony2 functional tests + Doctrine fixtures: Changes to entity won't save after flushing in doctrine fixtures

我在Symfony 2.6中执行功能测试的fixture时遇到了一个问题。在fixture中,我有一个用于创建基本实体并将其刷新到数据库的函数。然后我从其他函数中调用这个函数,这些函数将新创建的实体更改为更复杂的东西(比如设置与其他实体的关系),然后我再次刷新。

但是,当我检查数据库时,第二次刷新对实体没有任何更改。

我试过调试,我看到的是UnitOfWork在第二次刷新之前不包含任何插入/更新计划,尽管我执行函数set....()。

你知道我做错了什么吗?

下面是节选自我的夹具:

class LoadTestApplications extends AbstractFixture implements OrderedFixtureInterface
{
    const APPLICATION_ID_NOT_SUBMITTED_VALID = 37;
    public function load(ObjectManager $manager)
    {
        //other lines ommitted
        $this->loadNotSubmittedValidApplication($manager);
        //other lines ommited
    }
    private function loadNotSubmittedValidApplication(ObjectManager $manager)
    {
        $application = $this->createAndGetFilledApplication($manager, self::APPLICATION_ID_NOT_SUBMITTED_VALID,
            'property-property-empty', 'GROUP_INSTALLER', 'application-application-not-submitted-valid',
            'person-default', 'person-default', 'person-default', 'person-default');
        /** @var Installer $installer */
        $installer = $this->getReference('installer-installer1');
        /** @var InstallerContractor $installerContractor */
        $installerContractor = $this->getReference('installer-installer-contractor1');
        $application->setInstaller($installer);
        $installer->addApplication($application);
        $application->setInstallerContractor($installerContractor);
        $installerContractor->addApplication($application);
        $manager->persist($installer);
        $manager->persist($installerContractor);
        $manager->persist($application);
        $manager->flush(); // this saves no changes
    }
    private function createAndGetFilledApplication(ObjectManager $manager, $applicationId, $propertyReference,
       $acg, $referenceName, $contactReference, $improverReference, $billPayerReference, $propertyOwnerReference)
    {
        $application = new Application();
        $application->setId($applicationId);
        // lots of other set...() functions

        // that's for forcing our own ID on the entity, so we can force only
        // some of the entities to be loaded in the fixture and the id of
        // the entity stays the same no matter how many other entities have
        // been loaded before. This is crucial in our functional tests, so
        // we can go to e.g. http://..../application/15/show
        /** @var ClassMetadata $metadata */
        $metadata = $manager->getClassMetadata(get_class($application));
        $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
        $manager->flush(); //this saves all changes from this function
        $this->addReference($referenceName, $application);
        return $application;
    }
}

任何帮助都是感激的,提前谢谢你!

好吧,这次是我犯傻了。

我们最近将Application - InstallerContractor关系更改为多对多(从之前的一对多),所以不用

setInstallerContractor()

我不得不使用

addInstallerContractor()

感谢DerStoffel的帮助,我们一定会尝试https://github.com/liip/LiipFunctionalTestBundle!