Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using <code>MAX(id)</code> is not a very best solution as it might cause a few problems. What happen if you delete the newest record in given category and then add a new one? It will have the very same ID as previous had.</p> <ol> <li>Composite primary keys (<code>type</code>, <code>id</code>) are not very well handled by Doctrine so far, so I suggest to create a classical primary key: integer, auto-increment key (at least for Doctrine's use). In addition you can always have a unique index on <code>type</code> and <code>type_id</code> columns and use it whenever you want.</li> <li><p>Create your own implementation for auto-increment of <code>type_id</code> column:</p> <ol> <li>As <code>type_id</code> is associated with <em>Type</em> you could store additional column in <code>type</code> table: <code>next_auto_increment</code>, which stores a value that will be used for next record.</li> <li><p>Whenever you create a new record in your table simply select value for it's <code>type_id</code> from <code>type</code> table:</p> <pre><code>$type = ...; $entity = new Entity(); $entity-&gt;setType($type); $entity-&gt;setTypeId($type-&gt;getNextAutoIncrement()); // btw: typeId is not a very best name $em-&gt;persist($entity); </code></pre></li> <li><p>You will also need a trigger in your database to increment a value of <code>next_auto_increment</code> every time you insert a record (MySQL example):</p> <pre><code>DELIMITER $$ CREATE TRIGGER increment_ai AFTER insert ON entity_table FOR EACH ROW BEGIN UPDATE type_table SET next_auto_increment = next_auto_increment + 1 WHERE id = NEW.type; END$$ DELIMITER ; </code></pre></li> </ol></li> </ol>
 

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