数据夹具引用不存在
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->addReference()
?
如果你没有addReference()
,你就不能getReference()
跳转到文档https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html
【讨论】:
实际上是的,我应该在插入视频数据夹具时调用 addReference() 并添加 Video 作为参考,然后调用 getReference() 谢谢以上是关于数据夹具引用不存在的主要内容,如果未能解决你的问题,请参考以下文章
Symfony / Fixtures “引用已经存在,使用方法 setReference 来覆盖它”