无法在 Symfony 3.4 中加载固定装置
Cannot load fixtures in Symfony 3.4
我正在使用 DoctrineFixturesBundle。根据 documentation,任何扩展 Fixture
的 class 都将自动连接,以便控制台知道如何处理它。然而,尽管我的固定装置 class 扩展了 Fixture
(下面的代码),但我不断收到以下错误:
Could not find any fixture services to load.
如果我尝试在我的 services.yml
文件中手动连接服务:
services:
AppBundle\DataFixtures\:
resource: '../../src/AppBundle/DataFixtures/'
tags: ['doctrine.fixture.orm']
我反而得到:
In FileLoader.php line 168:
The file "../../src/AppBundle/DataFixtures" does not exist (in: /path/to/project/app/config) in /path/to/project/app/config/services.yml (which is being imported from "/path/to/project/app/config/config.yml").
In FileLocator.php line 71:
The file "../../src/AppBundle/DataFixtures" does not exist (in: /path/to/project/app/config).
我的数据装置的路径是 /path/to/project/src/DataFixtures
。实际的 class 文件 (AppFixtures.php
) 是:
namespace AppBundle\DataFixtures;
use AppBundle\Entity\Category;
use AppBundle\Entity\Product;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
class AppFixtures extends Fixture
{
public function load(ObjectManager $em)
{
$category = new Category();
$category->setName("Test Category")
->setSlug()
->setDescription("This is only for test purposes");
$fabricProduct = new Product();
$fabricProduct->setName("Test OldProduct")
->setCanonicalPrice(2.45)
->setHasSale(false)
->setCategory($category)
->setSlug()
->setNumberInStock(45.25);
$em->persist($category);
$em->persist($fabricProduct);
$em->flush();
}
}
抢先回答明显的问题,是的,包已正确注册。我的 services.yml
文件有默认的自动装配:
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
对我做错了什么有什么想法吗?根据我找到的所有文档,这应该有效。
解决了...使用 Symfony 中默认服务连接的工作方式,它希望所有内容都在 src/AppBundle
目录中。我的固定装置位于该目录之外的目录中(但显然仍在项目目录中)。将我的 fixtures 目录移动为 src/AppBundle
的子目录修复了它。
我正在使用 DoctrineFixturesBundle。根据 documentation,任何扩展 Fixture
的 class 都将自动连接,以便控制台知道如何处理它。然而,尽管我的固定装置 class 扩展了 Fixture
(下面的代码),但我不断收到以下错误:
Could not find any fixture services to load.
如果我尝试在我的 services.yml
文件中手动连接服务:
services:
AppBundle\DataFixtures\:
resource: '../../src/AppBundle/DataFixtures/'
tags: ['doctrine.fixture.orm']
我反而得到:
In FileLoader.php line 168:
The file "../../src/AppBundle/DataFixtures" does not exist (in: /path/to/project/app/config) in /path/to/project/app/config/services.yml (which is being imported from "/path/to/project/app/config/config.yml").
In FileLocator.php line 71:
The file "../../src/AppBundle/DataFixtures" does not exist (in: /path/to/project/app/config).
我的数据装置的路径是 /path/to/project/src/DataFixtures
。实际的 class 文件 (AppFixtures.php
) 是:
namespace AppBundle\DataFixtures;
use AppBundle\Entity\Category;
use AppBundle\Entity\Product;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
class AppFixtures extends Fixture
{
public function load(ObjectManager $em)
{
$category = new Category();
$category->setName("Test Category")
->setSlug()
->setDescription("This is only for test purposes");
$fabricProduct = new Product();
$fabricProduct->setName("Test OldProduct")
->setCanonicalPrice(2.45)
->setHasSale(false)
->setCategory($category)
->setSlug()
->setNumberInStock(45.25);
$em->persist($category);
$em->persist($fabricProduct);
$em->flush();
}
}
抢先回答明显的问题,是的,包已正确注册。我的 services.yml
文件有默认的自动装配:
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
对我做错了什么有什么想法吗?根据我找到的所有文档,这应该有效。
解决了...使用 Symfony 中默认服务连接的工作方式,它希望所有内容都在 src/AppBundle
目录中。我的固定装置位于该目录之外的目录中(但显然仍在项目目录中)。将我的 fixtures 目录移动为 src/AppBundle
的子目录修复了它。