从不同数据库映射 Doctrine 中的实体时抛出 ReflectionException [英] ReflectionException is thrown when mapping entities in Doctrine from different databases

查看:40
本文介绍了从不同数据库映射 Doctrine 中的实体时抛出 ReflectionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在包含两个模块的 ZF2 应用程序中使用 Doctrine 2,每个模块都有自己的数据库.我需要使用跨数据库连接,以便我可以将一个模块中的实体与另一个模块中的实体相关联.这是设置的 UML 图.

I'm trying to use Doctrine 2 in a ZF2 application which contains two modules, each with its own database. I need to use cross-database joins so that I can associate entities from one module with entities in another. Here's a UML diagram of the setup.

我已经尝试在我的第一个实体中使用它(我已经删除了不相关的参数和 use 语句):

I've tried using this in my first entity (I've removed irrelevant parameters and use statements):

namespace ClientEntity;

/**
 * A website.
 *
 * @ORMEntity
 * @ORMTable(name="users.website")
 * ...
 * @property $server
 */
class Website extends BaseEntity {

    // Other class vars ... 

    /**
     * @ORMManyToOne(targetEntity="ServerEntityServer", inversedBy="websites")
     * @ORMJoinColumn(name="server_id", referencedColumnName="id")
     */
    protected $server;

这在我的服务器实体中:

And this in my server entity:

namespace ServerEntity;

/**
 * A server.
 *
 * @ORMEntity
 * @ORMTable(name="servers.server")
 * ...
 * @property $websites
 */
class Server extends BaseEntity {

   // Other class vars ... 

   /**
    * @ORMOneToMany(targetEntity="ClientEntityWebsite", mappedBy="server")
    */
   protected $websites;

当我创建一个新的网站实体(通过一个使用 DoctrineModuleFormElementObjectSelect 作为服务器关联的网络表单)时,这个映射完美地工作,但是当我去edit 一个现有的网站,这个 ReflectionException 被抛出:

This mapping works perfectly when I create a new website entity (via a web form which uses DoctrineModuleFormElementObjectSelect for the server association), but when I go to edit an existing website, this ReflectionException is thrown:

类服务器实体网站不存在

Class ServerEntityWebsite does not exist

完整的堆栈跟踪可以在这里找到.出于某种原因,当服务器实体从其与网站实体的关联访问时,它认为所有实体都存在于 ServerEntity 命名空间中,而不是 ClientEntity.我需要做什么来确保服务器实体在正确的模块命名空间中查找?

The full stack trace can be found here. For some reason, when the Server entity is accessed from its association with a Website entity, it thinks all of the entities exist in the ServerEntity namespace rather than ClientEntity. What do I need to do to make sure the Server entity looks in the correct module namespace?

CLI 命令 orm:info 产生:

The CLI command orm:info produces:

Found 7 mapped entities:
[OK]   ServerEntityServer
[OK]   ClientEntityRole
[OK]   ClientEntityWebsite
[OK]   ClientEntityUser
[OK]   ClientEntityClient
[OK]   ClientEntityPermission
[OK]   MessageEntityNotification

但是 orm:validate-schema 结果:

[Mapping]  OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current mapping file.

<小时>

我在每个模块的 module.config.php 中都有这个:

'driver' => array(
    __NAMESPACE__ . '_driver' => array(
        'class' => 'DoctrineORMMappingDriverAnnotationDriver',
        'cache' => 'array',
        'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
    ),
    'orm_default' => array(
        'drivers' => array(
            __NAMESPACE__ . 'Entity' => __NAMESPACE__ . '_driver'
        )
    )
)

推荐答案

我设法修复了它.在我的 ServerEntityServer 中,我有这些用于添加/删除网站的 getter/setter 函数:

I managed to fix it. In my ServerEntityServer, I had these getter/setter functions for adding/removing websites:

public function setWebsite(Website $website) 
{
   $this->websites->add($website);    
}

public function removeWebsite(Website $website)
{
   $this->websites->removeElement($website);
}

但是你需要指定完整的命名空间作为参数:

But you need to specify the full namespace as the argument:

public function setWebsite(ClientEntityWebsite $website) { ... }

这么愚蠢的错误!我发现了这个问题,因为我浏览了堆栈跟踪中的每个文件,并试图将我的实体类中的每个方法/参数保存到代理文件中(Doctrine/ORM/Proxy/ProxyFactory 中的第 223 行).php).

Such a stupid mistake! I found the issue because I trawled through every file in the stack-trace and got to the point where it was attempting to save every method/argument in my Entity class to a proxy file (line 223ish in Doctrine/ORM/Proxy/ProxyFactory.php).

这篇关于从不同数据库映射 Doctrine 中的实体时抛出 ReflectionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆