Skip over navigation
Documentation
You are currently viewing documentation for a previously released version of OroCRM. See the latest long-term support version.

How to Load Data Fixtures

Before your application contains an interface to create new tasks, you need to load them programmatically. In the OroPlatform, this can be done by creating classes that are placed in the Migrations/Data/ORM subdirectory of your bundle and that implement the FixtureInterface:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// src/AppBundle/Migrations/Data/ORM/LoadTasks.php
namespace AppBundle\Migrations\Data\ORM;

use AppBundle\Entity\Priority;
use AppBundle\Entity\Task;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

class LoadTasks implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        $majorPriority = new Priority();
        $majorPriority->setLabel('major');
        $manager->persist($majorPriority);

        $importantTask = new Task();
        $importantTask->setSubject('Important task');
        $importantTask->setDescription('This is an important task');
        $importantTask->setDueDate(new \DateTime('+1 week'));
        $importantTask->setPriority($majorPriority);
        $manager->persist($importantTask);

        $minorPriority = new Priority();
        $minorPriority->setLabel('minor');
        $manager->persist($minorPriority);

        $unimportantTask = new Task();
        $unimportantTask->setSubject('Unimportant task');
        $unimportantTask->setDescription('This is a not so important task');
        $unimportantTask->setDueDate(new \DateTime('+2 weeks'));
        $unimportantTask->setPriority($minorPriority);
        $manager->persist($unimportantTask);

        $manager->flush();
    }
}

Use the oro:migration:data:load command to load all fixtures that have not been loaded yet:

$ php app/console oro:migration:data:load

Tip

You can use the --dry-run option to first check which fixtures will be loaded:

$ php app/console oro:migration:data:load

Tip

You can create data fixtures that should only be loaded when you want to to present your application with some demo data. To do so place your data fixture classes in the Migrations/Data/Demo/ORM subdirectory of your bundle and use the --fixtures-type option of the oro:migration:data:load command to indicate that the demo data should be loaded:

$ php app/console oro:migration:data:load --fixtures-type=demo
Browse maintained versions:2.62.32.01.12
Forums
Back to top