2013-03-23 15 views
4

我有一個應用程序我的工作有與其他幾個實體關聯一個Media實體:SpeakerTagCategory加載數十個數據燈具在一個整潔的方式

在下面的代碼我已經展示了我爲創建一些測試數據而寫的一個工具。爲了設置和分配數據之間的衆多關係顯然是很長的。

public function load(ObjectManager $manager) 
{ 
    $videoType = new Mediatype(); 
    $videoType->setName('video'); 
    $videoType->setType('video'); 

    $manager->persist($videoType); 

    $speaker1 = new Speaker(); 
    $speaker1->setName('Joe Bloggs'); 

    $speaker1->setBiography('Joe Bloggs bio.'); 

    $manager->persist($speaker1); 

    $category1 = new Category(); 
    $category1->setName('PHP'); 
    $category1->setSlug('php'); 

    $manager->persist($category1); 

    $tag1 = new Tag(); 
    $tag1->setName('PHPNW'); 
    $tag1->setSlug('phpnw'); 

    $manager->persist($tag1); 

    $video1 = new Media(); 
    $video1->setMediatype($videoType); 
    $video1->setSpeakers(
     new ArrayCollection(
      array(
       $speaker1 
      ) 
     ) 
    ); 

    $video1->setCategories(
     new ArrayCollection(
      array(
       $category1 
      ) 
     ) 
    ); 

    $video1->setTags(
     new ArrayCollection(
      array(
       $tag1 
      ) 
     ) 
    ); 

    $video1->setDate(new \Datetime()); 
    $video1->setCreationDate(new \DateTime()); 
    $video1->setTitle('My video about PHP'); 
    $video1->setDescription('A video about PHP!'); 
    $video1->setContent('http://some.video-url.com'); 
    $video1->setLength('20:00:00'); 
    $video1->setRating(2.5); 
    $video1->setVisits(100); 
    $video1->setLanguage('EN'); 
    $video1->setHostName('PHP'); 
    $video1->setHostUrl('php'); 
    $video1->setStatus('pub'); 
    $manager->persist($video1); 
    $manager->flush(); 
} 

現在我要替換此夾具與真實的數據和裝載十幾Media實體在一個夾具。我可以複製並粘貼十幾次,並更改數據,但這很麻煩,難以維護。有沒有一種很好的方式來加載像這樣的相同類型的衆多實體?

+0

你見過Symfony發電機嗎? – 2013-03-23 10:44:24

+0

還有[KhepinYAMLFixturesBundle](https://github.com/khepin/KhepinYamlFixturesBundle),它允許你在YAML文件中定義你的燈具(因爲我相信發生器也是這樣)。 – 2013-03-23 10:51:31

+0

所以也許只是循環此代碼並用https://github.com/fzaninotto/Faker生成的代碼替換字符串? – Ziumin 2013-03-23 19:57:10

回答

3

我意識到doctrine/data-fixtures包已經做到了我想要的。

要做到這一點我加載每個實體在自己的夾具,做$this->addReference('admin-user', $user);從使用$this->getReference('admin-user');

加載夾具是依賴其他設備進行訪問它是太容易了:

public function getDependencies() 
{ 
    // fixture classes that this fixture is dependent on 
    return array('MyDataFixtures\MyOtherFixture'); 
} 

所以現在我的夾具看起來像這樣:

public function load(ObjectManager $manager) 
{ 
    $video1 = new Media(); 
    $video1->setMediatype($this->getReference('video')); 
    $video1->setSpeakers(
     new ArrayCollection(
      array(
       $this->getReference('joe-bloggs') 
      ) 
     ) 
    ); 

    $video1->setCategories(
     new ArrayCollection(
      array(
       $this->getReference('php') 
      ) 
     ) 
    ); 

    $video1->setTags(
     new ArrayCollection(
      array(
       $this->getReference('phpnw') 
      ) 
     ) 
    ); 

    $video1->setDate(new \Datetime()); 
    $video1->setCreationDate(new \DateTime()); 
    $video1->setTitle('My video about PHP'); 
    $video1->setDescription('A video about PHP!'); 
    $video1->setContent('http://some.video-url.com'); 
    $video1->setLength('20:00:00'); 
    $video1->setRating(2.5); 
    $video1->setVisits(100); 
    $video1->setLanguage('EN'); 
    $video1->setHostName('PHP'); 
    $video1->setHostUrl('php'); 
    $video1->setStatus('pub'); 

    $manager->persist($video1); 
    $manager->flush(); 
} 

/** 
* Load this fixtures dependencies 
* @see https://github.com/doctrine/data-fixtures 
* 
* @return array 
*/ 
public function getDependencies() 
{ 
    return array(
     '...\LoadMediatypeData', 
     '...\LoadSpeakerData', 
     '...\LoadCategoryData', 
     '...\LoadTagData' 
    ); 
} 
+1

請注意,您需要擴展'Doctrine \ Common \ DataFixtures \ AbstractFixture'並實現'Doctrine \ Common \ DataFixtures \ OrderedFixtureInterface'以使此設置生效。 – Chris 2015-10-02 00:29:07

相關問題