src/Company/BaseBundle/DataFixtures/ORM/LoadDocumentData.php
changeset 61 9f427e7c88f9
parent 58 624e5900f5a4
child 64 97ddfdc8bdde
equal deleted inserted replaced
60:c8790bc6417a 61:9f427e7c88f9
    11 
    11 
    12 use Symfony\Component\DependencyInjection\ContainerInterface;
    12 use Symfony\Component\DependencyInjection\ContainerInterface;
    13 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
    13 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
    14 use Doctrine\Common\DataFixtures\FixtureInterface;
    14 use Doctrine\Common\DataFixtures\FixtureInterface;
    15 use Company\BaseBundle\Entity\Document;
    15 use Company\BaseBundle\Entity\Document;
       
    16 use Company\BaseBundle\Entity\Category;
    16 
    17 
    17 class LoadDocumentData implements FixtureInterface, ContainerAwareInterface
    18 class LoadDocumentData implements FixtureInterface, ContainerAwareInterface
    18 {
    19 {
    19     private $container;
    20     private $container;
    20 
    21 
    23         $this->container = $container;
    24         $this->container = $container;
    24     }
    25     }
    25     
    26     
    26     public function load($manager) {
    27     public function load($manager) {
    27         
    28         
       
    29         # create new categories
       
    30         $cat_def_list = array('cat1' => null, 'cat2' => null, 'cat3'=> null);
       
    31     
       
    32         foreach(array_keys($cat_def_list) as $cat_name) {
       
    33             $newcat = new Category();
       
    34             $newcat->setName($cat_name);
       
    35             $manager->persist($newcat);
       
    36             $cat_def_list[$cat_name] = $newcat;
       
    37         }
       
    38         
    28         # create new document
    39         # create new document
       
    40         $doc_def_list = array(
       
    41             array('title'=>'Title 1', 'description'=>'Description 1', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'categories' => array_values($cat_def_list)),
       
    42             array('title'=>'Title 2', 'description'=>'Description 2', 'tags' => array('tag2', 'tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'], $cat_def_list['cat2'])),
       
    43             array('title'=>'Title 3', 'description'=>'Description 3', 'tags' => array('tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'])),
       
    44             array('title'=>'Title 4', 'description'=>'Description 4', 'tags' => array(), 'categories' => array()),
       
    45             array('title'=>'Title 5', 'description'=>'Description 5', 'tags' => array('tag2', 'tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'], $cat_def_list['cat2'])),
       
    46             array('title'=>'Title 10', 'description'=>'Description 10', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'categories' => array()),
       
    47         );
    29         
    48         
    30         $doc_def_list = array(
    49         
    31             array('title'=>'Title 1', 'description'=>'Description 1', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4')),
    50         $newdocs = array();
    32             array('title'=>'Title 2', 'description'=>'Description 2', 'tags' => array('tag2', 'tag3', 'tag4')),
       
    33             array('title'=>'Title 3', 'description'=>'Description 3', 'tags' => array('tag3', 'tag4')),
       
    34             array('title'=>'Title 4', 'description'=>'Description 4', 'tags' => array()),
       
    35         );
       
    36         
    51         
    37         foreach ($doc_def_list as $doc_def) {
    52         foreach ($doc_def_list as $doc_def) {
    38             
    53             
    39             $newdoc = new Document();
    54             $newdoc = new Document();
    40             $newdoc->setTitle($doc_def['title']);
    55             $newdoc->setTitle($doc_def['title']);
    41             $newdoc->setDescription($doc_def['description']);
    56             $newdoc->setDescription($doc_def['description']);
    42                        
    57 
       
    58             foreach($doc_def['categories'] as $cat) {
       
    59                 $newdoc->getCategories()->add($cat);
       
    60             }
       
    61             
    43             $manager->persist($newdoc);
    62             $manager->persist($newdoc);
    44             
    63             
    45             $manager->flush();
    64             $newdocs[] = array($newdoc, $doc_def['tags']);
    46             
    65             
    47             $this->container->get('wiki_tag.document')->addTags($newdoc->getId(), $doc_def['tags']);
       
    48         }
    66         }
    49         
       
    50         # add tags
       
    51         
    67         
    52         $manager->flush();
    68         $manager->flush();
    53         
    69         
       
    70         foreach ($newdocs as $newdoc_array) {
       
    71             $newdoc = $newdoc_array[0];
       
    72             $tags = $newdoc_array[1];
       
    73             $this->container->get('wiki_tag.document')->addTags($newdoc->getId(), $tags);
       
    74             $manager->flush();
       
    75         }
       
    76         
       
    77         $manager->flush();
    54     }
    78     }
    55  
    79  
    56 }
    80 }