【问题标题】:Different entity/proxy dirs for Doctrine 2Doctrine 2 的不同实体/代理目录
【发布时间】:2011-03-15 14:23:33
【问题描述】:

我在我的项目中使用了学说 2,但我将我的所有模块分成了不同的文件夹,因此学说 2 实体根据它们所属的模块位于不同的目录中。

我想知道是否可以在使用现有的数据库连接时更改 Doctrine2 实体和代理目录设置。我查看了 EntityManager 类,但看不到更新配置的功能。

如果没有内置解决方案,有谁知道这个函数是否可以在 EntityManager 类中工作:

public function updateConfiguration(Configuration $config)
{
    $this->config = $config;

    $metadataFactoryClassName = $config->getClassMetadataFactoryName();
    $this->metadataFactory = new $metadataFactoryClassName;
    $this->metadataFactory->setEntityManager($this);
    $this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl());

    $this->proxyFactory = new ProxyFactory($this,
            $config->getProxyDir(),
            $config->getProxyNamespace(),
            $config->getAutoGenerateProxyClasses());
}

【问题讨论】:

    标签: php doctrine-orm


    【解决方案1】:

    我不清楚你的确切意思,但我会试一试。

    数据库连接是 EntityManager 的一部分,但它们并不相同。据我所知,如果连接已经建立,则无法更改数据库连接。

    在创建 EntityManger 实例之前,您必须确保已设置所有配置。您可以根据需要灵活地定义配置,但是一旦创建了 entitymanager,您就无法更改它(如果我错了,请纠正我)。如果您这样做,可能会导致已加载的其他实体出现问题,例如,您的刷新调用可能会失败。

    如果你想从不同的位置加载实体,你可以使用 Doctrine 类加载器。将模块的所有实体分配给命名空间并从文件系统上的任何位置加载该命名空间。

    小代码示例

    // Doctrine module
    $classLoader = new \Doctrine\Common\ClassLoader('Doctrine', "/var/www/library/Doctrine/");
    $classLoader->register();
    
    // User modules
    $classLoader = new \Doctrine\Common\ClassLoader('User', "/var/www/modules/models/User/");
    $classLoader->register();
    
    // Page module
    $classLoader = new \Doctrine\Common\ClassLoader('Page', "/some/path/to/different/modules/models/Page/");
    $classLoader->register();
    

    我看不到在请求期间更改 EntityManager 设置的用途。如果你必须这样做,你试图在错误的地方解决你的问题。我什至从未尝试过,也不想尝试:)。

    也许我不明白你的问题。如果是这样,请告诉我:)。

    【讨论】:

      【解决方案2】:

      可以像这样从实体管理器中检索 $config:

      $config = $em->getConfiguration();
      

      要动态更新实体路径试试这个(我自己没试过):

      $driverImpl = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(array(
          APP_PATH . DIRECTORY_SEPARATOR .  'entities'
      ));
      $config->setMetadataDriverImpl($driverImpl);
      

      P.S> 我认为这应该可行,但我没有尝试过,如果有错误请纠正我。

      【讨论】:

        猜你喜欢
        • 2012-12-13
        • 2011-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多