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

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

问题描述

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



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

 命名空间Client\Entity; 

/ **
*一个网站。
*
* @ ORM\Entity
* @ ORM\Table(name =users.website)
* ...
* @property $服务器
* /
类网站扩展BaseEntity {

//其他类vars ...

/ **
* @ORM \ManyToOne(targetEntity =Server\Entity\Server,inversedBy =websites)
* @ ORM\JoinColumn(name =server_id,referencedColumnName =id)
* /
protected $ server;

这在我的服务器实体中:

 命名空间Server\Entity; 

/ **
*服务器。
*
* @ ORM\Entity
* @ ORM\Table(name =servers.server)
* ...
* @property $网站
* /
类服务器扩展BaseEntity {

//其他类vars ...

/ **
* @ORM \OneToMany(targetEntity =Client\Entity\Website,mappedBy =server)
* /
protected $ sites;

当我创建一个新的网站实体(通过使用 DoctrineModule\Form\Element\ObjectSelect 用于服务器关联),但是当我转到编辑现有网站时,会抛出此ReflectionException: p>


类Server \Entity\Website不存在


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



CLI命令 orm:info 产生:

 找到7个映射实体:
[OK] Server\Entity\Server
[OK] Client\Entity\Role
[OK] Client\Entity\Website
[OK] Client\Entity\User
[OK] Client\ Entity\Client
[OK] Client\Entity\Permission
[OK] Message\Entity\Notification

但是 orm:validate-schema 导致:

  [Mapping] OK  - 映射文件是正确的。 
[数据库] FAIL - 数据库模式与当前映射文件不同步。






我在每个模块的 module.config.php

 'driver'=>数组(
__NAMESPACE__。'_driver'=>数组(
'class'=>'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache'= "'array',
'paths'=>数组(__ DIR__。'/../src/'。__NAMESPACE__。'/ Entity')
),
'orm_default'= >数组(
'drivers'=>数组(
__NAMESPACE__。'\Entity'=> __NAMESPACE__'_driver'




解决方案

我设法解决了。在我的 Server\Entity\Server 中,我有这些用于添加/删除网站的getter / setter功能:

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

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

但是您需要将完整命名空间指定为参数:

  public function setWebsite(\Client\Entity\Website $ website){...} 

这样的愚蠢的错误!我发现这个问题是因为我拖曳了stack-trace中的每个文件,并且到达了尝试将Entity类中的每个方法/参数保存到代理文件(Doctrine / ORM / Proxy / ProxyFactory中的第223ish行)。 php)。


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.

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

namespace Client\Entity;

/**
 * A website.
 *
 * @ORM\Entity
 * @ORM\Table(name="users.website")
 * ...
 * @property $server
 */
class Website extends BaseEntity {

    // Other class vars ... 

    /**
     * @ORM\ManyToOne(targetEntity="Server\Entity\Server", inversedBy="websites")
     * @ORM\JoinColumn(name="server_id", referencedColumnName="id")
     */
    protected $server;

And this in my server entity:

namespace Server\Entity;

/**
 * A server.
 *
 * @ORM\Entity
 * @ORM\Table(name="servers.server")
 * ...
 * @property $websites
 */
class Server extends BaseEntity {

   // Other class vars ... 

   /**
    * @ORM\OneToMany(targetEntity="Client\Entity\Website", mappedBy="server")
    */
   protected $websites;

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

Class Server\Entity\Website does not exist

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 Server\Entity namespace rather than Client\Entity. What do I need to do to make sure the Server entity looks in the correct module namespace?

The CLI command orm:info produces:

Found 7 mapped entities:
[OK]   Server\Entity\Server
[OK]   Client\Entity\Role
[OK]   Client\Entity\Website
[OK]   Client\Entity\User
[OK]   Client\Entity\Client
[OK]   Client\Entity\Permission
[OK]   Message\Entity\Notification

But orm:validate-schema results in:

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


I have this in each one of my module's module.config.php:

'driver' => array(
    __NAMESPACE__ . '_driver' => array(
        'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
        'cache' => 'array',
        'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
    ),
    'orm_default' => array(
        'drivers' => array(
            __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
        )
    )
)

解决方案

I managed to fix it. In my Server\Entity\Server, 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(\Client\Entity\Website $website) { ... }

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天全站免登陆