试图从命名空间"AppDataFixtures"加载类"BaseFixture"。您是否忘记了另一个命名空间的"use"语句?



我正在尝试使用以下命令:

php bin/console do:fixtures:load

在我的项目中添加一些虚拟数据。

但是,当我调用该命令时,我收到以下错误。

致命错误:未捕获的 Symfony\组件\调试\异常\ClassNotFound异常:试图 从命名空间"App\DataFixtures"加载类"BaseFixture"。你 忘记另一个命名空间的"use"语句?在 K:\programming\development\testProject\app\src\DataFixtures\UserFixtures.php 在第 9 行

Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to 从命名空间"App\DataFixtures"加载类"BaseFixture"。你 忘记另一个命名空间的"use"语句?在 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 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;
}

}

这是我的用户类,它扩展了基类。

<?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%d@example.com', $i));
$user->setFirstName($this->faker->firstName);
return $user;
});
// $product = new Product();
// $manager->persist($product);
$manager->flush();
}
}

我盯着这个看了很久,我似乎想不通问题出在哪里。

当然,此时如果尝试加载用户装置,Symfony控制台将抛出错误,因为App\Entity\User类尚不存在。

https://programarivm.com/why 你不应该将真实数据用于开发目的

最新更新