Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine detaching, caching, and merging
    primarykey
    data
    text
    <p>I'm on Doctrine 2.3. I have the following query:</p> <pre><code>$em-&gt;createQuery(' SELECT u, c, p FROM Entities\User u LEFT JOIN u.company c LEFT JOIN u.privilege p WHERE u.id = :id ')-&gt;setParameter('id', $identity) </code></pre> <p>I then take that, get the result (which is an array, I just take the first element), and run detach <code>$em-&gt;detach($result);</code>.</p> <p>When I go to fetch from the cache (using Doctrine's APC cache driver), I do:</p> <pre><code>$cacheDriver = new \Doctrine\Common\Cache\ApcCache(); if($cacheDriver-&gt;contains($cacheId)) { $entity = $cacheDriver-&gt;fetch($cacheId); $em-&gt;merge($entity); return $entity; } </code></pre> <p>My hope was that this would re-enable the relationship loading on the entity as there are many other relationships tied to the User object other than what's shown in that query.</p> <p>I'm trying to create a new entity like such:</p> <pre><code>$newEntity = new Entities\ClientType(); $newEntity['param'] = $data; $newEntitiy['company'] = $this-&gt;user['company']; $em-&gt;persist($newEntity); $em-&gt;flush(); </code></pre> <p>When I do this, I get an error:</p> <pre><code>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. </code></pre> <p>This works just fine when I don't use the company entity under the user entity that I got from cache. Is there any way to make this work so I don't have to refetch the company entity from the database every time I want to use it in a relationship with a new entity?</p> <p><strong>Edit:</strong> This is what I have in my User entity dealing with these two relationships:</p> <pre><code>/** * @ManyToOne(targetEntity="Company" , inversedBy="user", cascade={"detach", "merge"}) */ protected $company; /** * @ManyToOne(targetEntity="Privilege" , inversedBy="user", cascade={"detach", "merge"}) */ protected $privilege; </code></pre> <p>I am still getting the same error.</p> <p><strong>Second edit:</strong> Trying a <code>$em-&gt;contains($this-&gt;user);</code> and <code>$em-&gt;contains($this-&gt;user['company']);</code> both return false. Which sounds... wrong.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload