数据夹具参考不存在

Data fixture reference does not exist

我正在 symfony 中为应用程序中的视频功能编写数据夹具,以插入一些测试数据(视频评论) 这是代码。将数据添加到测试数据库的夹具 class:

<?php
namespace XOZ\AppBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use XOZ\AppBundle\DataFixtures\OrderHelper;
use XOZ\AppBundle\Entity\Comment;

class CreateVideoCommentTestData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
    use VideoListDataTrait;

    /**
     * @var ContainerInterface
     */
    private $container;
    private $comment_service;

    public function getOrder()
    {
        return OrderHelper::create()->requiresUser()->requiresVideoShoutOut()->get();
    }

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
        $this->comment_service = $this->container->get('xoz_app.comment_service');
    }

    public function load(ObjectManager $manager)
    {
        $videos = $this->getVideoList();

        foreach ($videos as $name => $key) {
            $song = $this->getReference($name);

            $message = 'I am a video comment';

            // comment on all songs
            $this->comment_service->addVideoShoutOutComment($this->getReference('Deletable'), $song, $message, true);
            $this->comment_service->addVideoShoutOutComment($this->getReference('AutoMute'), $song, $message, true);
            $this->comment_service->addVideoShoutOutComment($this->getReference('AutoMuteExempt'), $song, $message, true);
            $this->comment_service->addVideoShoutOutComment($this->getReference('Blocked'), $song, $message, true);

            // comment on these songs
            if ($name === 'My Metal Song') {
                $this->comment_service->addVideoShoutOutComment($this->getReference('User2'), $song, $message, true);
            }

            // comment on User1's songs
            if( $key === 'User1' ) {
                $this->comment_service->addVideoShoutOutComment($this->getReference('User1'), $song, $message, true);
                $this->comment_service->addVideoShoutOutComment($this->getReference('User2'), $song, $message, true);
            }

            // comment on User2's songs
            if( $key === 'User2' ) {
                $this->comment_service->addVideoShoutOutComment($this->getReference('Superuser'), $song, $message, true);
                $this->comment_service->addVideoShoutOutComment($this->getReference('User1'), $song, $message, true);
                $this->comment_service->addVideoShoutOutComment($this->getReference('User1'), $song, $message, true);
            }

            // comment on BlockedUser's songs
            if( $key === 'Blocked' ) {
                $this->comment_service->addVideoShoutOutComment($this->getReference('User1'), $song, $message, true);
                $this->comment_service->addVideoShoutOutComment($this->getReference('User1'), $song, $message, true);
            }
        }
    }
}

这是我正在使用的特征:

<?php

namespace XOZ\AppBundle\DataFixtures\ORM;

trait VideoListDataTrait
{
    public function getVideoList()
    {
        return [
            'My Metal VideoShoutOut'         => 'Superuser',
            'My Rock VideoShoutOut'          => 'Superuser',
            'My Blackmetal VideoShoutOut'    => 'Superuser',
            'My Trashmetal VideoShoutOut'    => 'Superuser',
            'My Coremetal VideoShoutOut'     => 'Superuser',
            'My HipHop VideoShoutOut'        => 'User1',
            'My DrumAndBass VideoShoutOut'   => 'User2',
            'Blocked User VideoShoutOut'     => 'Blocked',
            'AutoMute User VideoShoutOut'    => 'AutoMute',
            'AutoMuteExempt User VideoShoutOut'    => 'AutoMuteExempt',
            'CrewSuperAdminVideoShoutOut1'   => 'CrewSuperAdmin',
            'CrewSuperAdminVideoShoutOut2'   => 'CrewSuperAdmin',
            'CrewSuperAdminVideoShoutOut3'   => 'CrewSuperAdmin',
            'CrewSuperAdminVideoShoutOut4'   => 'CrewSuperAdmin',
            'CrewSuperAdminVideoShoutOut5'   => 'CrewSuperAdmin',
            'CrewAdminVideoShoutOut1'        => 'CrewAdmin',
            'CrewUserVideoShoutOut1'         => 'CrewUser',
            'CrewUserVideoShoutOut2'         => 'CrewUser',
            'CrewNonMemberVideoShoutOut1'    => 'CrewNonMember1',
            'CrewBlockedVideoShoutOut1'      => 'CrewBlocked1',
            'CrewBlockedVideoShoutOut2'      => 'CrewBlocked2',
        ];
    }
}

但是在加载数据夹具时出现此错误:

[OutOfBoundsException]                                 
  Reference to: (My Metal VideoShoutOut) does not exist

我做错了什么?

你在哪里调用函数$this->addReference()

如果你没有addReference()

,你就不能getReference()

跳转到文档https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html