如何删除对学说的弃用 AbstractFixture::getReference()

how to remove deprecation on doctrine AbstractFixture::getReference()

提问人:Garfield 提问时间:9/15/2023 更新时间:9/15/2023 访问量:67

问:

在 PHPUnit 10 中, 我在实体之间有一些固定装置,它们共享引用。 我有以下弃用,我无法删除它:

Argument $class of Doctrine\Common\DataFixtures\AbstractFixture::getReference() will be mandatory in 2.0. (AbstractFixture.php:95 called by ProductFixture.php:26, https://github.com/doctrine/data-fixtures/pull/409, package doctrine/data-fixtures)

这是我的代码,以及尝试删除弃用的不同步骤,但这得到了一个异常

class ProductFamilyFixture extends Fixture
{
    public const PRODUCT_FAMILY_1_REFERENCE = 'PRODUCTS_FAMILY_1_REFERENCE';

    /**
     * @param ObjectManager $manager
     */
    public function load(ObjectManager $manager): void
    {
        //*******************************************
        //ProductsFamilies

        $entity = new ProductsFamilies();
        $entity->setDesignation('Famille1');
        $manager->persist($entity);

        $entity2 = new ProductsFamilies();
        $entity2->setDesignation('Famille2');
        $manager->persist($entity2);

        $this->addReference(self::PRODUCT_FAMILY_1_REFERENCE, $entity);
        $manager->flush();
    }
}


    class ProductFixture extends Fixture  implements DependentFixtureInterface
{
    public const PRODUCT_1_REFERENCE = 'PRODUCT_1_REFERENCE';

    /**
     * @param ObjectManager $manager
     */
    public function load(ObjectManager $manager): void
    {
        //*******************************************

        $entity = new Products();
        $entity->setPn('Pn1')
            ->setDesignation('Désignation produit1')
            ->setPrefix('Pr')
            // work but send deprecation !!
            ->setFamily($this->getReference(ProductFamilyFixture::PRODUCT_FAMILY_1_REFERENCE)); 

            // This  doesn't work => error
            //OutOfBoundsException: Reference to "PRODUCTS_FAMILY_1_REFERENCE" for class "App\DataFixtures\ProductFamilyFixture" does not exist
        
//            ->setFamily($this->getReference(ProductFamilyFixture::PRODUCT_FAMILY_1_REFERENCE,ProductFamilyFixture::class)); 
//            ->setFamily($this->getReference('PRODUCT_FAMILY_1_REFERENCE','ProductFamilyFixture'));
//            ->setFamily($this->getReference('PRODUCT_FAMILY_1_REFERENCE',ProductFamilyFixture::class));
//            ->setFamily($this->getReference(ProductFamilyFixture::PRODUCT_FAMILY_1_REFERENCE,'ProductFamilyFixture'));

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

        $this->addReference(self::PRODUCT_1_REFERENCE, $entity);

    }

    public function getDependencies(): array
    {
        return [
            ProductFamilyFixture::class,
        ];
    }
}

有任何想法可以删除此弃用吗? 谢谢

symfony doctrine phpunit fixtures 弃用-警告

评论


答:

0赞 Dylan KAS 9/15/2023 #1

正如弃用警告中提到的,您必须在使用getReference()

$this->addReference($referenceName, $entity);

因为它需要您的实体类作为第二个参数

public function getReference($name, ?string $class = null)

如果为 null,则引发弃用警告。$class

只需使用如下所示的内容更新您的代码:

->setFamily($this->getReference(ProductFamilyFixture::PRODUCT_FAMILY_1_REFERENCE, ProductFamily::class)); 

评论

0赞 Garfield 9/15/2023
谢谢你睁开我的眼睛看我的错误。我传递了灯具的类以供参考,而不是实体的类。