Skip to content

Instantly share code, notes, and snippets.

@muzfr7
Last active August 17, 2017 17:34
Show Gist options
  • Save muzfr7/04c760732e92b034d2d6184d19f8cb7f to your computer and use it in GitHub Desktop.
Save muzfr7/04c760732e92b034d2d6184d19f8cb7f to your computer and use it in GitHub Desktop.
Sample Doctrine Fixture to default load users in database!
<?php
namespace AppBundle\DataFixtures\ORM;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use AppBundle\Entity\Workspace;
/**
* LoadWorkspaces Fixture
*
* To load users just execute this command: bin/console doctrine:fixtures:load
* By default the load command purges the database, removing all data from every table.
* To append your fixtures' data specify the --append option.
*
* DoctrineFixturesBundle should be installed beforehand..
*/
class LoadWorkspaces extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$workspace1 = new Workspace();
$workspace1->setName('Writing');
$workspace1->setDescription('info for writing Workspace');
$manager->persist($workspace1);
$manager->flush();
$this->addReference('workspace-writing', $workspace1);
}
public function getOrder()
{
return 10; // the order in which fixtures will be loaded
}
}
<?php
namespace AppBundle\DataFixtures\ORM;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use AppBundle\Entity\Project;
class LoadProjects extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$project1 = new Project();
$project1->setTitle('Symfony book');
$project1->setDescription('Some descriptions for Symfony book project');
$project1->setDueDate(new \DateTime('2014-10-20'));
$project1->setWorkspace($manager->merge($this->getReference('workspace-writing')));
$manager->persist($project1);
$manager->flush();
$this->addReference('project-symfony', $project1);
}
public function getOrder()
{
return 20; // the order in which fixtures will be loaded
}
}
<?php
namespace AppBundle\DataFixtures\ORM;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use AppBundle\Entity\Task;
class LoadTasks extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$task1 = new Task();
$task1->setTitle('writing chapter 1');
$task1->setDescription('descriptions for writing ch1');
$task1->setDueDate(new \DateTime('2014-10-14'));
$task1->setProject($manager->merge($this->getReference('project-symfony')));
$task1->setUser($manager->merge($this->getReference('user-muzafar')));
$task2 = new Task();
$task2->setTitle('reviewing chapter 1');
$task2->setDescription('descriptions for reviewing ch1');
$task2->setDueDate(new \DateTime('2014-10-16'));
$task2->setProject($manager->merge($this->getReference('project-symfony')));
$task2->setUser($manager->merge($this->getReference('user-javed')));
$task3 = new Task();
$task3->setTitle('editing chapter 1');
$task3->setDescription('descriptions for editing ch1');
$task3->setDueDate(new \DateTime('2014-10-18'));
$task3->setProject($manager->merge($this->getReference('project-symfony')));
$task3->setUser($manager->merge($this->getReference('user-javed')));
$manager->persist($task1);
$manager->persist($task2);
$manager->persist($task3);
$manager->flush();
}
public function getOrder()
{
return 40; // the order in which fixtures will be loaded
}
}
<?php
namespace AppBundle\DataFixtures\ORM;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use AppBundle\Entity\User;
class LoadUsers extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$user1 = new User();
$user1->setName('Muzafar Ali');
$user1->setBio('PHP Developer');
$user1->setEmail('[email protected]');
$user2 = new User();
$user2->setName('Javed Ali');
$user2->setBio('Accountant');
$user2->setEmail('[email protected]');
$manager->persist($user1);
$manager->persist($user2);
$manager->flush();
$this->addReference('user-muzafar', $user1);
$this->addReference('user-javed', $user2);
}
public function getOrder()
{
return 30; // the order in which fixtures will be loaded
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment