src/Company/BaseBundle/DataFixtures/ORM/LoadDocumentData.php
author ymh <ymh.work@gmail.com>
Mon, 19 Dec 2011 17:50:05 +0100
changeset 61 9f427e7c88f9
parent 58 624e5900f5a4
child 64 97ddfdc8bdde
permissions -rw-r--r--
Correct event mamangement in hdabo_sf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * This file is part of the WikiTagBundle package.
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 *
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * file that was distributed with this source code.
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 */
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
namespace Company\BaseBundle\DataFixures\ORM;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
use Symfony\Component\DependencyInjection\ContainerInterface;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use Doctrine\Common\DataFixtures\FixtureInterface;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use Company\BaseBundle\Entity\Document;
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    16
use Company\BaseBundle\Entity\Category;
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
class LoadDocumentData implements FixtureInterface, ContainerAwareInterface
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
{
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    private $container;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    public function setContainer(ContainerInterface $container = null)
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    {
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        $this->container = $container;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
    }
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    public function load($manager) {
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    29
        # create new categories
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    30
        $cat_def_list = array('cat1' => null, 'cat2' => null, 'cat3'=> null);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    31
    
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    32
        foreach(array_keys($cat_def_list) as $cat_name) {
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    33
            $newcat = new Category();
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    34
            $newcat->setName($cat_name);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    35
            $manager->persist($newcat);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    36
            $cat_def_list[$cat_name] = $newcat;
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    37
        }
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    38
        
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        # create new document
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        $doc_def_list = array(
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    41
            array('title'=>'Title 1', 'description'=>'Description 1', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'categories' => array_values($cat_def_list)),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    42
            array('title'=>'Title 2', 'description'=>'Description 2', 'tags' => array('tag2', 'tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'], $cat_def_list['cat2'])),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    43
            array('title'=>'Title 3', 'description'=>'Description 3', 'tags' => array('tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'])),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    44
            array('title'=>'Title 4', 'description'=>'Description 4', 'tags' => array(), 'categories' => array()),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    45
            array('title'=>'Title 5', 'description'=>'Description 5', 'tags' => array('tag2', 'tag3', 'tag4'), 'categories' => array($cat_def_list['cat1'], $cat_def_list['cat2'])),
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    46
            array('title'=>'Title 10', 'description'=>'Description 10', 'tags' => array('tag1', 'tag2', 'tag3', 'tag4'), 'categories' => array()),
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        );
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    49
        
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    50
        $newdocs = array();
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    51
        
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        foreach ($doc_def_list as $doc_def) {
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            $newdoc = new Document();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            $newdoc->setTitle($doc_def['title']);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            $newdoc->setDescription($doc_def['description']);
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    57
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    58
            foreach($doc_def['categories'] as $cat) {
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    59
                $newdoc->getCategories()->add($cat);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    60
            }
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    61
            
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            $manager->persist($newdoc);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    64
            $newdocs[] = array($newdoc, $doc_def['tags']);
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        }
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        $manager->flush();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        
61
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    70
        foreach ($newdocs as $newdoc_array) {
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    71
            $newdoc = $newdoc_array[0];
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    72
            $tags = $newdoc_array[1];
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    73
            $this->container->get('wiki_tag.document')->addTags($newdoc->getId(), $tags);
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    74
            $manager->flush();
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    75
        }
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    76
        
9f427e7c88f9 Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents: 58
diff changeset
    77
        $manager->flush();
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    }
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
 
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
}