php - Doctrine 映射字段不起作用

标签 php doctrine

我有两个实体 User\UserMisc\Notification 我想通过 获取用户实体内部的用户通知$用户->getNotifications()。 通常我对这种关系没有问题。

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

用户\用户:属性

/**
 * 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;

用户\用户:__construct()

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

杂项\通知:属性

/**
 * @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 没有任何改变)。 我认为我的问题可能是 Doctrine 缓存问题,所以我尝试清除缓存:

app/console doctrine:cache:clear-metadata

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

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

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

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 映射的。所以该字段从未被初始化。如果您使用另一个缓存器,那可能是一个类似的问题。

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

sudo service apache2 restart

阿们。

关于php - Doctrine 映射字段不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28860956/

相关文章:

php - 如何获取从 PUT 方法传递的数据

php - 与 db 的 Doctrine 联系

php/symfony/doctrine 内存泄漏?

php - Symfony 3 找不到 pdo_mysql 驱动程序

php - 根据选定的数据库条目显示数据库中的数据

php - 自定义字段 - 从真/假到是/否的 bool 值显示

php - Doctrine: No alias was set before invoking getRootAlias() 错误

symfony - 在 symfony 中删除实体时删除文件

mongodb - 使用 MongoDB 配置 Sonata Media Bundle 时出现问题

PHP - 如何从添加日期算起 60 天