1

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??

yivi
  • 42,438
  • 18
  • 116
  • 138
H3lltronik
  • 562
  • 8
  • 26

1 Answers1

1

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 ?

Yoann MIR
  • 811
  • 5
  • 17
  • Yes, it will be a reusable bundle installed via composer from a git repository, some project might have it, the ones that requires a database interaction, for now it is in the root folder for development, when its finished I will upload it to a repository and then install it as a vendor package. About the psr-4 namespace I already did it so Im able to use classes from my bundle in my project, yet I cannot load the fixtures from there with the fixtures command – H3lltronik Dec 04 '19 at 15:32
  • 1
    Is there a tag you must set up in your service declaration ? About working on your bundle in the vendor directory, here is a some tips:https://carlosbuenosvinos.com/working-at-the-same-time-in-a-project-and-its-dependencies-composer-and-path-type-repository/ & https://medium.com/pvtl/local-composer-package-development-47ac5c0e5bb4 – Yoann MIR Dec 04 '19 at 16:54
  • 2
    Also may be related to your problem : https://stackoverflow.com/questions/50984433/could-not-find-any-fixture-services-to-load – Yoann MIR Dec 04 '19 at 17:03