DataFixtures/ORM/LoadDocumentData.php
author ymh <ymh.work@gmail.com>
Fri, 26 Sep 2014 10:04:40 +0200
changeset 136 f209fcb0df6c
parent 80 cf6a88559482
permissions -rwxr-xr-x
Added tag V00.18 for changeset ab728cb3632c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * file that was distributed with this source code.
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
namespace IRI\Bundle\WikiTagBundle\DataFixures\ORM;
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
use Symfony\Component\DependencyInjection\ContainerInterface;
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use Doctrine\Common\DataFixtures\FixtureInterface;
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use Company\BaseBundle\Entity\Document;
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
use Company\BaseBundle\Entity\Category;
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
class LoadDocumentData implements FixtureInterface, ContainerAwareInterface
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
{
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    private $container;
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    public function setContainer(ContainerInterface $container = null)
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    {
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        $this->container = $container;
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    }
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    public function load($manager) {
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        # create new categories
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        $cat_def_list = array('cat1' => null, 'cat2' => null, 'cat3'=> null);
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        foreach(array_keys($cat_def_list) as $cat_name) {
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
            $newcat = new Category();
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            $newcat->setName($cat_name);
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
            $manager->persist($newcat);
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
            $cat_def_list[$cat_name] = $newcat;
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        }
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        # create new document
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        $doc_def_list = array(
80
cf6a88559482 Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents: 69
diff changeset
    41
            array('title'=>'Title 0', 'description'=>'Description 0', 'tags' => null, 'categories' => array()),
69
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
            array('title'=>'Title 1', 'description'=>'Description 1', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'categories' => array_values($cat_def_list)),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            array('title'=>'Title 2', 'description'=>'Description 2', 'tags' => array('tag2', 'tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'], $cat_def_list['cat2'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
            array('title'=>'Title 3', 'description'=>'Description 3', 'tags' => array('tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            array('title'=>'Title 4', 'description'=>'Description 4', 'tags' => array(), 'categories' => array()),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            array('title'=>'Title 5', 'description'=>'Description 5', 'tags' => array('tag2', 'tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'], $cat_def_list['cat2'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            array('title'=>'Title 10', 'description'=>'Description 10', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'categories' => array()),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            array('title'=>'Title 11', 'description'=>'Description 11', 'tags' => array('tag4'), 'categories' => array($cat_def_list['cat2'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            array('title'=>'Title 20', 'description'=>'Description 20', 'tags' => array('newtag1'), 'categories' => array($cat_def_list['cat3'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
            array('title'=>'Title 21', 'description'=>'Description 21', 'tags' => array('newtag2'), 'categories' => array($cat_def_list['cat3'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
            array('title'=>'Title 22', 'description'=>'Description 22', 'tags' => array('another'), 'categories' => array($cat_def_list['cat3'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            array('title'=>'Title 23', 'description'=>'Description 23', 'tags' => array('hello world'), 'categories' => array($cat_def_list['cat3'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            array('title'=>'Lorem ipsum dolor sit amet', 'description'=>'Morbi adipiscing luctus ullamcorper. Nam suscipit, turpis vel faucibus fringilla, sem leo fermentum nunc, et mattis leo urna sed tellus. Suspendisse consectetur turpis cursus ipsum ullamcorper gravida. Nullam arcu nisi, condimentum id condimentum non, lobortis nec lorem. Donec commodo, ligula sit amet posuere fermentum, urna elit faucibus nunc, et faucibus lorem erat quis urna. Vestibulum a quam eros. Suspendisse non felis a metus faucibus porta. Morbi adipiscing augue vel justo euismod non pulvinar sem posuere. Duis sit amet ipsum et quam cursus commodo eu a purus. Pellentesque gravida tempus libero, eu consectetur nisl posuere id.', 'tags' => array('foobar'), 'categories' => array($cat_def_list['cat3'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            array('title'=>'Lorem ipsum dolor sit amet', 'description'=>'Lorem ipsum ullamcorper', 'tags' => array('barfoo'), 'categories' => array($cat_def_list['cat3'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            array('title'=>'caveat', 'description'=>'emptor', 'tags' => array('hello world'), 'categories' => array($cat_def_list['cat3'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            array('title'=>'emptor', 'description'=>'caveat', 'tags' => array('bonjour monde'), 'categories' => array($cat_def_list['cat3'])),
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        );
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        $newdocs = array();
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        foreach ($doc_def_list as $doc_def) {
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            $newdoc = new Document();
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            $newdoc->setTitle($doc_def['title']);
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            $newdoc->setDescription($doc_def['description']);
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
            foreach($doc_def['categories'] as $cat) {
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
                $newdoc->getCategories()->add($cat);
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            }
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
            
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
            $manager->persist($newdoc);
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
            
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
            $newdocs[] = array($newdoc, $doc_def['tags']);
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        }
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        $manager->flush();
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
        
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
        foreach ($newdocs as $newdoc_array) {
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            $newdoc = $newdoc_array[0];
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            $tags = $newdoc_array[1];
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            $this->container->get('wiki_tag.document')->addTags($newdoc->getId(), $tags);
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            $manager->flush();
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        }
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        $manager->flush();
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    }
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
 
d76e5e0ded24 Move Data Fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
}