我在教义2.3。我有以下查询:
$em->createQuery('
SELECT u, c, p
FROM Entities\User u
LEFT JOIN u.company c
LEFT JOIN u.privilege p
WHERE u.id = :id
')->setParameter('id', $identity)
然后我接受它,得到结果(这是一个数组,我只取第一个元素),然后运行 detach $em->detach($result);
。
当我从缓存中获取(使用 Doctrine 的 APC 缓存驱动程序)时,我会:
$cacheDriver = new \Doctrine\Common\Cache\ApcCache();
if($cacheDriver->contains($cacheId))
{
$entity = $cacheDriver->fetch($cacheId);
$em->merge($entity);
return $entity;
}
我希望这将重新启用实体上的关系加载,因为除了该查询中显示的内容之外,还有许多其他关系与 User 对象相关联。
我正在尝试创建一个像这样的新实体:
$newEntity = new Entities\ClientType();
$newEntity['param'] = $data;
$newEntitiy['company'] = $this->user['company'];
$em->persist($newEntity);
$em->flush();
当我这样做时,我得到一个错误:
A new entity was found through the relationship 'Entities\ClientType#company' that was not configured to cascade persist operations for entity:
Entities\Company@000000005d7b49b500000000dd7ad743.
To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).
If you cannot find out which entity causes the problem implement 'Entities\Company#__toString()' to get a clue.
当我不使用从缓存中获得的用户实体下的公司实体时,这工作得很好。有什么办法可以使这项工作,这样我就不必每次想在与新实体的关系中使用它时都从数据库中重新获取公司实体?
编辑: 这就是我在处理这两种关系的用户实体中所拥有的:
/**
* @ManyToOne(targetEntity="Company" , inversedBy="user", cascade={"detach", "merge"})
*/
protected $company;
/**
* @ManyToOne(targetEntity="Privilege" , inversedBy="user", cascade={"detach", "merge"})
*/
protected $privilege;
我仍然遇到同样的错误。
第二次编辑:
尝试 a$em->contains($this->user);
并且$em->contains($this->user['company']);
两者都返回 false。这听起来……错了。