条令映射领域不起作用


Doctrine mapped field is not working

我有两个实体User'UserMisc'Notification,我希望通过执行$user->getNotifications(),可以在user实体中获取用户的通知。通常我对这种关系没有意见。

请参阅下面的代码(我刚刚添加了TicketApp关系作为示例,因为这种关系是有效的,并且完全相同):

User'User : properties

/**
 * This is working
 * @ORM'OneToMany(targetEntity="[...]'Entity'Ticket'TicketApp", mappedBy="author")
 */
private $tickets;
/**
 * This is not
 * @ORM'OneToMany(targetEntity="[...]'Entity'Misc'Notification", mappedBy="receiver", fetch="EXTRA_LAZY")
 */
private $notifications;

User'User : __construct()

$this->tickets       = new 'Doctrine'Common'Collections'ArrayCollection();
$this->notifications = new 'Doctrine'Common'Collections'ArrayCollection();

Misc'Notification : properties

/**
 * @ORM'ManyToOne(targetEntity="[...]'Entity'User'User", inversedBy="notifications")
 * @ORM'JoinColumn(nullable=false)
 */
private $receiver;

这似乎还好(或者今晚我完全失明了…)。问题是:$user->getNotifications()返回null,而不是Doctrine Collection对象。我的表里有数据。这种类型的代码工作并返回两个通知:

$em->getRepository('[...]:Misc'Notification')->findByReceiver($user);

此外,我在Symfony调试工具栏中注意到,使用$user->getNotifications()时不会触发任何查询

当我想起Doctrine有自己的缓存,独立于Symfony时,我对找到解决方案感到绝望(我尝试了几十个cache:clear,没有任何更改)。我认为我的问题可能是条令缓存问题,所以我试图清除缓存:

app/console doctrine:cache:clear-metadata

(如果你是独立使用条令的,这个命令在纯条令中有它的等价物,Symfony只是重命名和改进条令命令)。

如果你有一个正常的安装,这应该工作,否则继续阅读。

在正常情况下,至少在开发模式下,Doctrine会在每次请求时重新生成元数据缓存。但是Doctrine也有一个APC接口来存储其缓存。。。我两三周前激活了它,现在我已经完全忘记了。当我运行上面的命令时,条令说:

Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.

现在问题很清楚了。条令填充了缓存,然后无法正确删除数据。getNotifications方法返回null,因为它是纯PHP,但Doctrine不知道notifications属性是SQL映射的。因此,字段从未初始化。如果你使用另一个纪念符,这可能是一个类似的问题。

上面给出的错误是不言自明的。您需要清除APC数据存储。由于它是由Web服务器进程托管的,只需重新启动这个家伙:

sudo service apache2 restart

阿门。