我正在尝试在ZF2应用程序中使用Doctrine 2,该应用程序包含两个模块,每个模块都有自己的数据库。我需要使用跨数据库连接,以便我可以将一个模块中的实体与另一个模块中的实体相关联。 Here's a UML diagram设置。
我尝试在我的第一个实体中使用它(我删除了不相关的参数和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;
当我创建一个新的网站实体时(通过使用DoctrineModule\Form\Element\ObjectSelect
进行服务器关联的网络表单),这种映射非常有效,但是当我去编辑现有网站时,这个抛出ReflectionException:
Class Server \ Entity \ Website不存在
完整堆栈跟踪can be found here。出于某种原因,当从与网站实体的关联访问服务器实体时,它认为所有实体都存在于Server\Entity
命名空间而不是Client\Entity
中。我需要做些什么才能确保Server实体查找正确的模块名称空间?
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'
)
)
)
答案 0 :(得分:3)
我设法解决了这个问题。在我的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) { ... }
这样一个愚蠢的错误!我发现了这个问题,因为我浏览了堆栈跟踪中的每个文件,并且试图将我的Entity类中的每个方法/参数保存到代理文件(Doctrine / ORM / Proxy / ProxyFactory中的223ish行)。 PHP)。