Note that there are some explanatory texts on larger screens.

plurals
  1. POSkip Entities while flushing when they are a Duplicate
    text
    copied!<p>i'm playing a little bit with Symfony2 and Doctrine2.</p> <p>I have an Entity that has a unique title for example:</p> <pre><code>class listItem { /** * @orm:Id * @orm:Column(type="integer") * @orm:GeneratedValue(strategy="AUTO") */ protected $id; /** * @orm:Column(type="string", length="255", unique="true") * @assert:NotBlank() */ protected $title; </code></pre> <p>now i'm fetching a json and updating my database with those items:</p> <pre><code>$em = $this-&gt;get('doctrine.orm.entity_manager'); foreach($json-&gt;value-&gt;items as $item) { $listItem = new ListItem(); $listItem-&gt;setTitle($item-&gt;title); $em-&gt;persist($listItem); } $em-&gt;flush(); </code></pre> <p>works fine the first time. but the second time i'm getting an sql error (of course): <code>Integrity constraint violation: 1062 Duplicate entry</code></p> <p>sometimes my json file gets updated and some of the items are new, some are not. Is there a way to tell the entity manager to skip the duplicate files and just insert the new ones?</p> <p>Whats the best way to do this?</p> <p>Thanks for all help. Please leave a comment if something is unclear</p> <h2>Edit:</h2> <p>what works for me is doing something like this:</p> <pre><code>$uniqueness = $em-&gt;getRepository('ListItem')-&gt;checkUniqueness($item-&gt;title); if(false == $uniqueness) { continue; } $listItem = new ListItem(); $listItem-&gt;setTitle($item-&gt;title); $em-&gt;persist($listItem); $em-&gt;flush(); } </code></pre> <p><code>checkUniqueness</code> is a method in my ListItem Repo that checks if the title is already in my db.</p> <p>thats horrible. this are 2 database queries for each item. this ends up about 85 database queries for this action.</p>
 

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