数据夹具引用不存在

Posted

技术标签:

【中文标题】数据夹具引用不存在【英文标题】:Data fixture reference does not exist 【发布时间】:2018-12-05 10:11:09 【问题描述】:

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

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

你在哪里调用函数 $this-&gt;addReference()

如果你没有addReference(),你就不能getReference()

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

【讨论】:

实际上是的,我应该在插入视频数据夹具时调用 addReference() 并添加 Video 作为参考,然后调用 getReference() 谢谢

以上是关于数据夹具引用不存在的主要内容,如果未能解决你的问题,请参考以下文章

Symfony / Fixtures “引用已经存在,使用方法 setReference 来覆盖它”

MySQL 中可能存在或不存在的引用数据

“PL/SQL:ORA-02289:序列不存在”在 django 中进行加载数据。数据库是远程oracle 12c

删除数据框中引用另一个 (R) 中不存在的 ID 的行?

外键的引用说表不存在

MySQL删除具有引用不存在ID的参数的行