我使用的是Doctrine 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;
}
我的希望是这样做能够重新启用实体上的关系加载,因为除了该查询中显示的内容之外,还有许多其他与用户对象相关联的关系。
我正在尝试创建一个新的实体,如下所示:
$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;
我仍然遇到相同的错误。
第二次编辑:尝试使用$em->contains($this->user);
和$em->contains($this->user['company']);
都返回false。听起来...不对。
cascade={"detach", "merge"}
,对吗? - nwalke