【问题标题】:Symfony2 functional tests + Doctrine fixtures: Changes to entity won't save after flushing in doctrine fixturesSymfony2 功能测试 + Doctrine 固定装置:在原则固定装置中刷新后实体的更改不会保存
【发布时间】:2015-06-03 07:59:23
【问题描述】:

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

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

我尝试过调试,但我看到 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;
    }
}

感谢您的帮助,提前感谢您!

【问题讨论】:

  • 这可以帮助您管理用于测试的夹具:github.com/liip/LiipFunctionalTestBundle您确定以正确的顺序执行它们吗?此外,如果实体不是新创建的,则无需调用 $manager->persist();冲洗就足够了。

标签: php symfony doctrine-orm functional-testing fixtures


【解决方案1】:

好吧,这次是我傻了。

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

setInstallerContractor()

我不得不使用

addInstallerContractor()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多