Tigran Muradyan
Tigran Muradyan

Reputation: 512

Symfony can't find data fixture

I have created a data fixture class to load some data.

It is in src/AppBundle/DataFixtures/ORM/FixtureLoader.php

<?php
namespace AppBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use AppBundle\Entity\User;
use AppBundle\Entity\Role;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;

class FixtureLoader implements FixtureInterface
{
    public function load($manager)
    {
        // создание роли ROLE_ADMIN
        $role = new Role();
        $role->setName('ROLE_ADMIN');

        $manager->persist($role);

        // создание пользователя
        $user = new User();
        $user->setFirstName('Tigran');
        $user->setLastName('Muradyan');
        $user->setEmail('[email protected]');
        $user->setUsername('stereoshoots');
        $user->setSalt(md5(time()));

        // шифрует и устанавливает пароль для пользователя,
        // эти настройки совпадают с конфигурационными файлами
        $encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
        $password = $encoder->encodePassword('stereoshoots', $user->getSalt());
        $user->setPassword($password);

        $user->getUserRoles()->add($role);

        $manager->persist($user);
    }
}

I'm using next commands to load that fixture:

php app/console doctrine:fixtures:load

php app/console doctrine:fixtures:load --fixtures=src\AppBundle\DataFixtures\ORM --append

It can't my FixtureLoader.php.

Upvotes: 0

Views: 147

Answers (1)

scoolnico
scoolnico

Reputation: 3135

First, add use Doctrine\Common\Persistence\ObjectManager;

and add ObjectManager in the load method signature: public function load(ObjectManager $manager) { ... }

To finish, flush your entities:

public function load(ObjectManager $manager) {

     /***/

     $manager->flush();

}

Upvotes: 1

Related Questions