尝试从命名空间“App\DataFixtures”加载类“BaseFixture”。您是不是忘记了另一个名称空间的“使用”语句?
Posted
技术标签:
【中文标题】尝试从命名空间“App\\DataFixtures”加载类“BaseFixture”。您是不是忘记了另一个名称空间的“使用”语句?【英文标题】:Attempted to load class "BaseFixture" from namespace "App\DataFixtures". Did you forget a "use" statement for another namespace?尝试从命名空间“App\DataFixtures”加载类“BaseFixture”。您是否忘记了另一个名称空间的“使用”语句? 【发布时间】:2020-05-24 10:10:15 【问题描述】:我正在尝试使用命令:
php bin/console do:fixtures:load
在我的项目中添加一些虚拟数据。
当我调用命令时,我收到以下错误。
致命错误:未捕获 Symfony\Component\Debug\Exception\ClassNotFoundException:试图 从命名空间“App\DataFixtures”加载类“BaseFixture”。你是否 忘记另一个命名空间的“使用”语句?在 K:\programming\development\testProject\app\src\DataFixtures\UserFixtures.php 在第 9 行
Symfony\Component\Debug\Exception\ClassNotFoundException: 试图 从命名空间“App\DataFixtures”加载类“BaseFixture”。你是否 忘记另一个命名空间的“使用”语句?在 K:\programming\development\testProject\app\src\DataFixtures\UserFixtures.php 在第 9 行
调用堆栈: 0.1044 2464280 1. Symfony\Component\Debug\ErrorHandler->handleException() K:\programming\development\testProject\app\vendor\symfony\debug\ErrorHandler.php:0
我似乎无法找出问题所在。
这是我的BaseFixtures
课堂
<?php
namespace App\DataFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Migrations\Version\Factory;
use Symfony\Bundle\MakerBundle\Generator;
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;
这是我扩展基类的用户类。
<?php
namespace App\DataFixtures;
use App\Entity\User;
use App\DataFixtures\BaseFixture;
use Doctrine\Common\Persistence\ObjectManager;
class UserFixtures extends BaseFixture
protected function loadData(ObjectManager $manager)
$this->createMany(10, 'main_users', function ($i)
$user = new User();
$user->setEmail(sprintf('email%d@example.com', $i));
$user->setFirstName($this->faker->firstName);
return $user;
);
// $product = new Product();
// $manager->persist($product);
$manager->flush();
我已经盯着这个看了很长时间,我似乎无法弄清楚问题出在哪里。
【问题讨论】:
您确定文件名称正确吗? 【参考方案1】:当然,此时如果尝试加载用户设备,Symfony 控制台会抛出错误,因为 App\Entity\User 类还不存在。
https://programarivm.com/why-you-shouldnt-use-real-data-for-development-purposes
【讨论】:
您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。以上是关于尝试从命名空间“App\DataFixtures”加载类“BaseFixture”。您是不是忘记了另一个名称空间的“使用”语句?的主要内容,如果未能解决你的问题,请参考以下文章
尝试从命名空间加载类“Sm_Image_HandlerController”您是不是忘记了另一个命名空间的“使用”语句?
尝试从命名空间“Application\MediaBundle\Entity”加载类“Media”