私は 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;
}
そのクエリに表示されているもの以外に、 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.
これは、キャッシュから取得したユーザー エンティティの下で会社エンティティを使用しない場合にうまく機能します。新しいエンティティとの関係で使用するたびにデータベースから会社エンティティを再取得する必要がないように、これを機能させる方法はありますか?
編集: これは、これら 2 つの関係を扱う User エンティティにあるものです。
/**
* @ManyToOne(targetEntity="Company" , inversedBy="user", cascade={"detach", "merge"})
*/
protected $company;
/**
* @ManyToOne(targetEntity="Privilege" , inversedBy="user", cascade={"detach", "merge"})
*/
protected $privilege;
私はまだ同じエラーが発生しています。
2 番目の編集:
a$em->contains($this->user);
と$em->contains($this->user['company']);
両方を試してみると、false が返されます。どちらが聞こえますか...間違っています。