原则映射字段不起作用
Doctrine mapped field is not working
我有两个实体 User\User
和 Misc\Notification
,我想通过执行 $user->getNotifications()
来获取用户实体内的用户通知。
通常我对这种关系没有问题。
查看下面的代码(我只是添加了 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()
returns null
而不是 Doctrine Collection
对象。
我的 table 中有数据。这种类型的代码有效 returns 两个通知:
$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接口来存放它的缓存。。。我两三周前激活过,现在已经完全忘记了。当我运行上面的命令时,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
阿们。
我有两个实体 User\User
和 Misc\Notification
,我想通过执行 $user->getNotifications()
来获取用户实体内的用户通知。
通常我对这种关系没有问题。
查看下面的代码(我只是添加了 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()
returns null
而不是 Doctrine Collection
对象。
我的 table 中有数据。这种类型的代码有效 returns 两个通知:
$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接口来存放它的缓存。。。我两三周前激活过,现在已经完全忘记了。当我运行上面的命令时,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
阿们。