H3lltronik
H3lltronik

Reputation: 886

Load fixtures from custom bundle

I'm building a simple bundle for Symfony and I was wondering if I can run fixtures located in there since it is not in the project src root folder. This is my project structure:

Project
|
+--AdminBundle <- My custom bundle
|  +--Controller
|  +--DataFixtures
|  |  |
|  |  +--AdminFixtures.php <- The fixtures I want to run
|  | 
|  +--DependencyInjection
|  +--Entity
|  +--Resources
|
+--assets
+--bin
+--config
+--public
+--src
+--templates
+--vendor

This is the code of AdminFixtures.php


namespace ExampleVendor\AdminBundle\DataFixtures;

use App\AdminBundle\Entity\User;
use App\AdminBundle\Entity\Profile;
use Doctrine\Bundle\FixturesBundle\Fixture;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class AdminFixtures extends Fixture implements FixtureGroupInterface {
    private $passwordEncoder;


    public function __construct(UserPasswordEncoderInterface $passwordEncoder) {
        $this->passwordEncoder = $passwordEncoder;
    }

    public function load(ObjectManager $manager) {
        // Fixtures stuff
    }

    /**
     * This method must return an array of groups
     * on which the implementing class belongs to
     *
     * @return string[]
     */
    public static function getGroups(): array {
        return ['admin'];
    }
}

When I run php bin/console doctrine:fixtures:load I get an error that says:

[ERROR] Could not find any fixture services to load.

I read something that every class that implements FixtureGroupInterface will be automatically registered as fixture but I think that is not working since I am here crying for help.

How can I register it manually as fixture? how to make php bin/console doctrine:fixtures:load command work??

Upvotes: 1

Views: 810

Answers (1)

Yoann MIR
Yoann MIR

Reputation: 821

I don't know the DoctrineFixtures bundle very well so every loaded class implementing a certain interface may eventually be used but i guess it's only possible if you make sure that classes outside of the src directory are loaded, take a look at your composer.json it should contain something like that :

................
    "autoload": {
        "psr-4": {
            "": "src/",
        }
    },

and think about adding something like that: (assuming you use "Admin\" as namespace for your classes in AdminBundle)

................
    "autoload": {
        "psr-4": {
            "": "src/",
            "Admin\\": "AdminBundle/",
        }
    },

Also, people who know symfony will eventually correct me but i am not sure you are supposed to create bundle like this anymore. Either Admin is a module of your app and you put it in src/Admin or you think about reusing it elsewhere and put it in your vendor as a proper library ?

Upvotes: 1

Related Questions