Doctrine映射字段不起作用

时间:2015-03-04 17:28:33

标签: php doctrine

我有两个实体User\UserMisc\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()返回null而不是Doctrine Collection对象。 我的表中有数据。这种类型的代码可以工作并返回两个通知:

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

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

1 个答案:

答案 0 :(得分:5)

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

app/console doctrine:cache:clear-metadata

(如果您使用的是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.

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

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

sudo service apache2 restart

阿门。