entity - Doctrine2, retry insert with different value on duplicate key -


i'm trying create hash. following path followed:

  1. create hash object unique key on $hash
  2. constructor fills in seed generate actual $hash
  3. try saving it
  4. if saving failed because of error 23000 (duplicate key), make different hash , repeat till unique hash made doesn't exist yet.

now here's problem. using doctrine2, closes entitymanager when query fails due sql error. in case that's no problem because retry it.

one solution search database if hash exists. due very, low amount of collision (md5) , need stuff fast possible (every ms can save worth it), want skip check.

another solution thought worth shot, clone entity manager. however, internally entity manager passed objects inside , not cloned passed reference.

a third solution use registry , create new entity manager. however, outside of object not have right entity manager:

  • object gets em -> query
  • object b gets em -> query > error > creates new em
  • object c gets em -> query new em
  • object > query > error, closed because got @ step 1.

what should able insert? need via entities, i'm not going make flat dql/sql queries.

how finding unique hash before persisting?

do {     $unique_hash = md5(microtime()); // work out hash here.     $existing = $repository->findonebyhash($unique_hash);     if (!$existing) {         break; // else carry on until one.     } } while (1);  // use unique hash here. 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -