从不同的数据库中学说映射实体时ReflectionException抛出(ReflectionExc

2019-07-20 06:42发布

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

我在我的第一个实体使用这个(我已经去除了无关紧要的参数,并试图use的语句):

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;

而这在我的服务器实体:

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;

这种映射完美的作品,当我创建一个新的网站实体(通过它使用Web表单DoctrineModule\Form\Element\ObjectSelect服务器协会),但是当我去编辑现有的网站,这ReflectionException抛出:

类Server \实体\网站不存在

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

CLI命令orm:info产生:

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

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' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
        'cache' => 'array',
        'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
    ),
    'orm_default' => array(
        'drivers' => array(
            __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
        )
    )
)

Answer 1:

我设法解决它。 在我的Server\Entity\Server ,我有添加/删除这些网站的getter / setter函数:

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

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

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

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

这样一个愚蠢的错误! 我发现这个问题,因为我通过在堆栈跟踪每个文件拖网,并获得到它试图将每一个方法/参数在我的实体类保存到代理文件(行223ish教义/ ORM /代理/ ProxyFactory里的点。 PHP)。



文章来源: ReflectionException is thrown when mapping entities in Doctrine from different databases