Load dozens of data in a neat manner

I have a Media object in the application I'm working on, has associations with several other objects: Speaker , Tag , Category , etc.

In the code below, I showed the tool that I wrote to create some test data. Obviously, there is a lot of time to set up and assign numerous relationships between data.

 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(); } 

Now I want to replace this device with real data and download about a dozen Media objects in one device. I could copy and paste it a dozen times and make changes to the data, but it's messy and harder to maintain. Is there a good way to load many objects of the same type like this?

+4
source share
1 answer

I realized that the doctrine / data-fixtures package is already doing exactly what I wanted.

To do this, I load each object into my own device and <<20> to access it from another device using $this->getReference('admin-user');

Downloading dependencies is also easy:

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

So now my tool is as follows:

 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' ); } 
+3
source

All Articles