如何在现有的未来 Symfony 应用程序中使用 Symfony2 的 Doctrine2?
How to use Doctrine2 from Symfony2 in an existing future-Symfony application?
我没有通过 Symfony 的文档和 google 找到任何解决方案。我只找到有关从头开始应用程序的内容。
我有一个大型自己的框架应用程序,我想慢慢迁移到 Symfony2。我没有从头开始构建新应用程序的便利。
到目前为止,我将整个 symfony 文件夹克隆到我自己的框架应用程序的根文件夹中。
我通过直接实现它来使用 Doctrine,没有接触 Symfony,如下所示。
<?php
namespace QueryBuild\Helpers;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
class Listing
{
protected $appConfig;
protected $doctrineEntityManager;
public function __construct(\cConfig $app_config)
{
$this->setAppConfig($app_config);
$this->setDoctrineEntityManager();
}
public function setAppConfig($app_config)
{
$this->appConfig = $app_config;
}
public function setDoctrineEntityManager()
{
$paths = array(
$this->getAppConfig()->BasePath() . "/symfony/src/El/Entity/"
);
$isDevMode = true;
// the connection configuration
$dbParams = array(
'driver' => 'sqlsrv', // pdo_sqlsrv for mssql
'host' => $this->getAppConfig()->db->Host,
'user' => $this->getAppConfig()->db->User,
'password' => $this->getAppConfig()->db->Password,
'dbname' => $this->getAppConfig()->db->Database
);
// 5th param on false, to NOT use the simple annotation reader. Otherwise annotations are going to be readed wrong. Can be due to server configurations.
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
$doctrineEntityManager = EntityManager::create($dbParams, $config);
$this->doctrineEntityManager = $doctrineEntityManager;
}
public function getAppConfig()
{
return $this->appConfig;
}
public function getDoctrineEntityManager()
{
return $this->doctrineEntityManager;
}
}
不要对我的命名空间感到困惑,我的实际任务是创建查询构建功能。
现在我不想再通过将登录数据传递到 setDoctrineEntityManager() 来连接到数据库。
我已经在 /symfony/app/config/parameters.yml 中写下了这些信息。
现在我的想法是实例化 symfony 以便能够读取配置,但是从 symfony 到实例化 doctrine。
我怎样才能做到这一点?
多谢指教。
最后我的解决方案和我之前想的相反。 Symfony2 现在是包装 基础应用程序,遗留应用程序(目前)被封装为 LegacyBundle。现在我可以通过例如遗留应用程序的实体管理器甚至控制器容器。
我没有通过 Symfony 的文档和 google 找到任何解决方案。我只找到有关从头开始应用程序的内容。
我有一个大型自己的框架应用程序,我想慢慢迁移到 Symfony2。我没有从头开始构建新应用程序的便利。 到目前为止,我将整个 symfony 文件夹克隆到我自己的框架应用程序的根文件夹中。 我通过直接实现它来使用 Doctrine,没有接触 Symfony,如下所示。
<?php
namespace QueryBuild\Helpers;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
class Listing
{
protected $appConfig;
protected $doctrineEntityManager;
public function __construct(\cConfig $app_config)
{
$this->setAppConfig($app_config);
$this->setDoctrineEntityManager();
}
public function setAppConfig($app_config)
{
$this->appConfig = $app_config;
}
public function setDoctrineEntityManager()
{
$paths = array(
$this->getAppConfig()->BasePath() . "/symfony/src/El/Entity/"
);
$isDevMode = true;
// the connection configuration
$dbParams = array(
'driver' => 'sqlsrv', // pdo_sqlsrv for mssql
'host' => $this->getAppConfig()->db->Host,
'user' => $this->getAppConfig()->db->User,
'password' => $this->getAppConfig()->db->Password,
'dbname' => $this->getAppConfig()->db->Database
);
// 5th param on false, to NOT use the simple annotation reader. Otherwise annotations are going to be readed wrong. Can be due to server configurations.
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
$doctrineEntityManager = EntityManager::create($dbParams, $config);
$this->doctrineEntityManager = $doctrineEntityManager;
}
public function getAppConfig()
{
return $this->appConfig;
}
public function getDoctrineEntityManager()
{
return $this->doctrineEntityManager;
}
}
不要对我的命名空间感到困惑,我的实际任务是创建查询构建功能。
现在我不想再通过将登录数据传递到 setDoctrineEntityManager() 来连接到数据库。 我已经在 /symfony/app/config/parameters.yml 中写下了这些信息。 现在我的想法是实例化 symfony 以便能够读取配置,但是从 symfony 到实例化 doctrine。
我怎样才能做到这一点? 多谢指教。
最后我的解决方案和我之前想的相反。 Symfony2 现在是包装 基础应用程序,遗留应用程序(目前)被封装为 LegacyBundle。现在我可以通过例如遗留应用程序的实体管理器甚至控制器容器。