20.08.11 - Fixtures in Symfony 2
How to create fixtures in our Symfony 2 project
The fixtures is a data configuration for our Symfony 2 project, is a simple but a powerful way to load into the database default or required information for the correct execution of the application. Often, in the development phase, the model changes or even dropped, as the recovery comes tedious the fixtures can save us hours of manual SQL insertions.
To install a Doctrine 2 fixtures system in our Symfony 2 app, we have to add the next lines to the "deps" file.
Installation:
We have to activate it in the app/autoload.php:And in the app/AppKernel.php:Generally, we also have to create a fixture file per entity defined in the model. We can create a fixture to load the default admin users of our app:
Finally, in order to load the fixtures configuration, we have to execute the next command:
To install a Doctrine 2 fixtures system in our Symfony 2 app, we have to add the next lines to the "deps" file.
[doctrine-fixtures]
git=http://github.com/doctrine/data-fixtures.git
[DoctrineFixturesBundle]
git=http://github.com/symfony/DoctrineFixturesBundle.git
target=/bundles/Symfony/Bundle/DoctrineFixturesBundle
git=http://github.com/doctrine/data-fixtures.git
[DoctrineFixturesBundle]
git=http://github.com/symfony/DoctrineFixturesBundle.git
target=/bundles/Symfony/Bundle/DoctrineFixturesBundle
$ bin/vendors install
$loader->registerNamespaces(array(
// ...
'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib',
'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
// ...
));
// ...
'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/doctrine-fixtures/lib',
'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
// ...
));
public function registerBundles()
{
$bundles = array(
// ...
new Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle(),
// ...
);
// ...
}
{
$bundles = array(
// ...
new Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle(),
// ...
);
// ...
}
<?php
# Test/MyBundle/DataFixtures/ORM/LoadUserData.php
namespace Test\MyBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Test\MyBundle\Entity\User;
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* Main method for fixtures insertion
*
* @param Doctrine\Manager $manager
*/
public function load($manager)
{
//Save the user
$user = new User();
$user->setUsername('admin');
$user->setPassword('admin');
$user->setLastLogin(new \DateTime('now'));
$user->setCreatedAt(new \DateTime('now'));
$user->setUpdatedAt(new \DateTime('now'));
$user->setIsActive(true);
$user->setRoleName(User::ROLE_SUPER_ADMIN);
$manager->persist($user);
$manager->flush();
//Associate a reference for other fixtures
$this->addReference('user-admin', $user);
}
/**
* Get the order of this execution
*
* @return int
*/
public function getOrder()
{
return 1;
}
}
# Test/MyBundle/DataFixtures/ORM/LoadUserData.php
namespace Test\MyBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Test\MyBundle\Entity\User;
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface
{
/**
* Main method for fixtures insertion
*
* @param Doctrine\Manager $manager
*/
public function load($manager)
{
//Save the user
$user = new User();
$user->setUsername('admin');
$user->setPassword('admin');
$user->setLastLogin(new \DateTime('now'));
$user->setCreatedAt(new \DateTime('now'));
$user->setUpdatedAt(new \DateTime('now'));
$user->setIsActive(true);
$user->setRoleName(User::ROLE_SUPER_ADMIN);
$manager->persist($user);
$manager->flush();
//Associate a reference for other fixtures
$this->addReference('user-admin', $user);
}
/**
* Get the order of this execution
*
* @return int
*/
public function getOrder()
{
return 1;
}
}
$ php app/console doctrine:fixtures:load
More information: Symfony

you love this? for more to your friends
root@xxx:/???/???/symblog.dev# php app/console doctrine:fixtures:load PHP Fatal error: Declaration of Blogger\BlogBundle\DataFixtures\ORM\BlogFixtures::load() must be compatible with that of Doctrine\Common\DataFixtures\FixtureInterface::load() in /???/???/symblog.dev/src/Blogger/BlogBundle/DataFixtures/ORM/BlogFixtures.php on line 10 Fatal error: Declaration of Blogger\BlogBundle\DataFixtures\ORM\BlogFixtures::load() must be compatible with that of Doctrine\Common\DataFixtures\FixtureInterface::load() in /???/???/symblog.dev/src/Blogger/BlogBundle/DataFixtures/ORM/BlogFixtures.php on line 10 Hi dude do you have any idea of how to solve this error i have follow several tuts but have no find a solution yet
FixtureInterface::loadlikepublic function load($manager)@Victor Hugo Update your BlogFixtures::load() method to look like this: public function load(\Doctrine\Common\Persistence\ObjectManager $manager)
I'm also having the same issue i.e "...must be compatible with that of Doctrine\Common\DataFixtures\FixtureInterface::load()". Please help.
@Victor Hugo: simply modify your load method like following: public function load(ObjectManager $manager) And add the related namespace: use Doctrine\Common\Persistence\ObjectManager; It should work then.