数据装置参考不存在

时间:2018-06-26 15:59:46

标签: symfony fixtures data-fitting

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

<?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

我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

您在哪里调用函数Set SQL = "Select '" + name+ "' as name,"+ age + "as age,'" + gender + "' as Gender

如果您没有$this->addReference(),则不能getReference()

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