我有两个实体 UserUser
和 MiscNotification
并且我希望有可能通过执行 在 User 实体中获取用户的通知$user->getNotifications()
.通常我对这种类型的关系没有问题.
I have two entities UserUser
and MiscNotification
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
* @ORMOneToMany(targetEntity="[...]EntityTicketTicketApp", mappedBy="author")
*/
private $tickets;
/**
* This is not
* @ORMOneToMany(targetEntity="[...]EntityMiscNotification", mappedBy="receiver", fetch="EXTRA_LAZY")
*/
private $notifications;
用户用户:__construct()
$this->tickets = new DoctrineCommonCollectionsArrayCollection();
$this->notifications = new DoctrineCommonCollectionsArrayCollection();
杂项通知:属性
/**
* @ORMManyToOne(targetEntity="[...]EntityUserUser", inversedBy="notifications")
* @ORMJoinColumn(nullable=false)
*/
private $receiver;
<小时>
这似乎没问题(或者今晚我完全失明了......).问题是: $user->getNotifications()
返回 null
而不是 Doctrine 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('[...]:MiscNotification')->findByReceiver($user);
另外,我注意到在 Symfony 调试工具栏中,使用 $user->getNotifications()
时不会触发任何查询.
Also, I noticed in the Symfony debug toolbar that no query is fired when using $user->getNotifications()
.
当我想起 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.
问题现在很清楚了.Doctrine 填充了缓存,然后无法正确删除数据.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 数据存储.由于它是由网络服务器进程托管的,只需重启这个人:
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
阿门.
这篇关于教义映射字段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!