Doctrine映射字段不起作用

Mor*_*ing 2 php doctrine

我有两个实体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;
Run Code Online (Sandbox Code Playgroud)

User\User : __construct()

$this->tickets       = new \Doctrine\Common\Collections\ArrayCollection();
$this->notifications = new \Doctrine\Common\Collections\ArrayCollection();
Run Code Online (Sandbox Code Playgroud)

Misc\Notification : properties

/**
 * @ORM\ManyToOne(targetEntity="[...]\Entity\User\User", inversedBy="notifications")
 * @ORM\JoinColumn(nullable=false)
 */
private $receiver;
Run Code Online (Sandbox Code Playgroud)

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

$em->getRepository('[...]:Misc\Notification')->findByReceiver($user);
Run Code Online (Sandbox Code Playgroud)

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

Mor*_*ing 7

当我记得Doctrine有适当的缓存时,我绝望地找到了解决方案,独立于Symfony(我试了几十次而cache:clear没有任何改变).我认为我的问题可能是Doctrine缓存问题,所以我试图清除缓存:

app/console doctrine:cache:clear-metadata
Run Code Online (Sandbox Code Playgroud)

(如果您使用的是Doctrine Standalone,此命令在纯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.
Run Code Online (Sandbox Code Playgroud)

现在问题很明显了.Doctrine填充缓存,然后无法正确删除数据.该getNotifications方法返回null是因为它是纯PHP,但是Doctrine并不知道该notifications属性是SQL映射的.所以该领域从未被初始化.如果你使用另一个cacher,这可能是一个类似的问题.

上面给出的错误是自我解释.您需要清除APC数据存储区.由于它是由webserver进程托管的,只需重新启动这个人:

sudo service apache2 restart
Run Code Online (Sandbox Code Playgroud)

阿门.