原则映射的字段不起作用 [英] Doctrine mapped field is not working

查看:153
本文介绍了原则映射的字段不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体 User\User Misc\Notification ,我希望有可能通过执行 $ user-> getNotifications()在User实体中获取用户的通知。
通常我对这种类型的关系没有问题。

I have two entities User\User and Misc\Notification and I want to have the possibility to fetch the user's notifications inside the User entity, by doing $user->getNotifications(). Usually I have no problem with this type of relations.

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

See my code below (I just added the TicketApp relation as an example, since this relation works and is exactly the same):

/**
 * 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()



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 而不是教义 Collection 对象。
我的表中有数据。这种类型的代码有效并返回两个通知:


This seems to be Ok (or maybe this evening I'm completely blind...). The problem is : $user->getNotifications() returns null instead of a Doctrine Collection object. I have datas in my table. This type of code works and returns two notifications:

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

此外,我在Symfony调试工具栏中注意到没有查询被触发使用 $ user-> getNotifications()时。

Also, I noticed in the Symfony debug toolbar that no query is fired when using $user->getNotifications().

推荐答案

I当我想起Doctrine拥有独立于Symfony的适当缓存时,我对寻找解决方案感到绝望(我尝试了数十个 cache:clear 而不作任何更改)。
我以为我的问题可能是Doctrine缓存问题,所以我尝试清除缓存:

I was despairing of finding a solution when I remembered that Doctrine have its proper cache, independent from Symfony (I tried tens of cache:clear without any change). I thought that my problem could be a Doctrine cache issue, so I tried to clear the cache:

app/console doctrine:cache:clear-metadata

(如果您使用的是独立的Doctrine,则此命令具有等效的功能在纯Doctrine中,Symfony只是重命名并改进了Doctrine命令。)

(If you are using Doctrine standalone, this command has its equivalent in pure Doctrine, Symfony just renames and improves Doctrine commands).

如果您的安装正常,则应该可以,否则继续阅读。

If you have a normal installation, this should work, otherwise continue to read.

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

In a normal time, in dev mode at least, Doctrine regenerates the metadata cache at each request. But Doctrine also have an APC interface to store its cache... I activated it two or three weeks ago, and I have completely forgotten it. When I ran the command above, Doctrine said:

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

问题现在很明显。原则会填满缓存,然后无法正确删除数据。 getNotifications 方法返回 null ,因为它是纯PHP,但是Doctrine不知道 notifications 属性已被SQL映射。因此,该字段从未初始化。如果您使用其他缓存器,则可能是类似的问题。

The problem is clear now. Doctrine fills the cache and then can't remove data correctly. The getNotifications method was returning null because it was pure PHP, but Doctrine had no knowledge that the notifications property was SQL-mapped. So the field were never initialized. If you use another cacher, that's probably a similar issue.

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

The error given above is self-explaining. You'll need to clear the APC datastore. Since it is hosted by the webserver process, just reboot this guy:

sudo service apache2 restart

阿门。

这篇关于原则映射的字段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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