Attempted to load class "BaseFixture" from namespace "AppDataFixtures". Did you forget a "use" statement for another namespace?

I am trying use the Command:

php bin/console do:fixtures:load

to add some dummy data in my Project.

When i call the command however i get the following error.

Fatal error: Uncaught SymfonyComponentDebugExceptionClassNotFoundException: Attempted to
load class “BaseFixture” from namespace “AppDataFixtures”. Did you
forget a “use” statement for another namespace? in
K:programmingdevelopmenttestProjectappsrcDataFixturesUserFixtures.php
on line 9

SymfonyComponentDebugExceptionClassNotFoundException: Attempted to
load class “BaseFixture” from namespace “AppDataFixtures”. Did you
forget a “use” statement for another namespace? in
K:programmingdevelopmenttestProjectappsrcDataFixturesUserFixtures.php
on line 9

Call Stack:
0.1044 2464280 1. SymfonyComponentDebugErrorHandler->handleException()
K:programmingdevelopmenttestProjectappvendorsymfonydebugErrorHandler.php:0

And i can’t seem to figure out the Problem.

Here is my BaseFixtures Class

  <?php

namespace AppDataFixtures;

use DoctrineBundleFixturesBundleFixture;
use DoctrineCommonPersistenceObjectManager;
use DoctrineMigrationsVersionFactory;
use SymfonyBundleMakerBundleGenerator;

abstract class BaseFixture extends Fixture
{
    /** @var ObjectManager */
    private $manager;

    /** @var Generator */
    protected $faker;

    private $referencesIndex = [];

    abstract protected function loadData(ObjectManager $manager);

    public function load(ObjectManager $manager)
    {
        $this->manager = $manager;
        $this->faker = Factory::create();

        $this->loadData($manager);
    }

    /**
     * Create many objects at once:
     *
     *      $this->createMany(10, function(int $i) {
     *          $user = new User();
     *          $user->setFirstName('Ryan');
     *
     *           return $user;
     *      });
     *
     * @param int      $count
     * @param string   $groupName Tag these created objects with this group name,
     *                            and use this later with getRandomReference(s)
     *                            to fetch only from this specific group.
     * @param callable $factory
     */
    protected function createMany(int $count, string $groupName, callable $factory)
    {
        for ($i = 0; $i < $count; $i++) {
            $entity = $factory($i);

            if (null === $entity) {
                throw new LogicException('Did you forget to return the entity object from your callback to BaseFixture::createMany()?');
            }

            $this->manager->persist($entity);

            // store for usage later as groupName_#COUNT#
            $this->addReference(sprintf('%s_%d', $groupName, $i), $entity);
        }
    }

    protected function getRandomReference(string $groupName) {
        if (!isset($this->referencesIndex[$groupName])) {
            $this->referencesIndex[$groupName] = [];

            foreach ($this->referenceRepository->getReferences() as $key => $ref) {
                if (strpos($key, $groupName.'_') === 0) {
                    $this->referencesIndex[$groupName][] = $key;
                }
            }
        }

        if (empty($this->referencesIndex[$groupName])) {
            throw new InvalidArgumentException(sprintf('Did not find any references saved with the group name "%s"', $groupName));
        }

        $randomReferenceKey = $this->faker->randomElement($this->referencesIndex[$groupName]);

        return $this->getReference($randomReferenceKey);
    }

    protected function getRandomReferences(string $className, int $count)
    {
        $references = [];
        while (count($references) < $count) {
            $references[] = $this->getRandomReference($className);
        }

        return $references;
    }

}

And here is my User Class which Extends the Base Class.

    <?php

namespace AppDataFixtures;

use AppEntityUser;
use AppDataFixturesBaseFixture;
use DoctrineCommonPersistenceObjectManager;

class UserFixtures extends BaseFixture
{
    protected function loadData(ObjectManager $manager)
    {
        $this->createMany(10, 'main_users', function ($i){
           $user = new User();
           $user->setEmail(sprintf('email%[email protected]', $i));
           $user->setFirstName($this->faker->firstName);

           return $user;
        });

        // $product = new Product();
        // $manager->persist($product);

        $manager->flush();
    }

}

I habe beem staring at this for a long time and i can’t seem to figure out what the problem is.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *