我使用过微内核,并尝试配置Doctrine ORM。
app/config/config.yml
framework:
secret: S0ME_SECRET
templating:
engines: ['twig']
profiler: { only_exceptions: false }
doctrine:
dbal:
driver: pdo_mysql
host: 127.0.0.1
dbname: symfony-micro
user: root
password: ''
orm:
app/appKernel.php
use SymfonyBundleFrameworkBundleKernelMicroKernelTrait;
use SymfonyComponentConfigLoaderLoaderInterface;
use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentHttpKernelKernel;
use SymfonyComponentRoutingRouteCollectionBuilder;
use DoctrineCommonAnnotationsAnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
class AppKernel extends Kernel
{
use MicroKernelTrait;
public function registerBundles()
{
$bundles = array(
new SymfonyBundleFrameworkBundleFrameworkBundle(),
new SymfonyBundleTwigBundleTwigBundle(),
new SensioBundleFrameworkExtraBundleSensioFrameworkExtraBundle(),
new DoctrineBundleDoctrineBundleDoctrineBundle()
);
if ($this->getEnvironment() == 'dev') {
$bundles[] = new SymfonyBundleWebProfilerBundleWebProfilerBundle();
}
return $bundles;
}
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config.yml');
if (isset($this->bundles['WebProfilerBundle'])) {
$c->loadFromExtension('web_profiler', array(
'toolbar' => true,
'intercept_redirects' => false,
));
}
}
protected function configureRoutes(RouteCollectionBuilder $routes)
{
if (isset($this->bundles['WebProfilerBundle'])) {
$routes->import('@WebProfilerBundle/Resources/config/routing/wdt.xml', '/_wdt');
$routes->import('@WebProfilerBundle/Resources/config/routing/profiler.xml', '/_profiler');
}
$routes->import(__DIR__.'/../src/App/Controller/', '/', 'annotation');
}
public function getCacheDir()
{
return __DIR__.'/../var/cache/'.$this->getEnvironment();
}
public function getLogDir()
{
return __DIR__.'/../var/logs';
}
}
src/App/Controller微控制器.php
namespace AppController;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use AppEntityArticle;
class MicroController extends Controller
{
/**
* @Route("/test/{limit}")
*/
public function testAction($limit)
{
$article = $this->getDoctrine()
->getRepository(Article::class)
->find(1);
echo '<pre>';
print_r($articles);
die;
}
}
src/App/Entity/Article.php
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
* Article
*
* @ORMTable(name="article")
* @ORMEntity(repositoryClass="AppRepositoryArticleRepository")
*/
class Article
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="content", type="text")
*/
private $content;
...
当我尝试从数据库中获取某些内容时,我得到:
班级
'AppEntityArticle' was not found in the chain configured namespaces
我认为问题出在ORM配置中。有人可以帮忙吗?
使用MicroKernelTrait
配置Doctrine ORM的推荐方法已经暴露在Symfony存储库的Doctrine配方中:
doctrine:
# ...
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'AppEntity'
alias: App
此配置应涵盖您的无束结构。