12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495 |
- <?php
-
-
- namespace Doctrine\ORM;
-
- use Exception, InvalidArgumentException, UnexpectedValueException,
- Doctrine\Common\Collections\ArrayCollection,
- Doctrine\Common\Collections\Collection,
- Doctrine\Common\NotifyPropertyChanged,
- Doctrine\Common\PropertyChangedListener,
- Doctrine\ORM\Event\LifecycleEventArgs,
- Doctrine\ORM\Mapping\ClassMetadata,
- Doctrine\ORM\Proxy\Proxy;
-
-
- class UnitOfWork implements PropertyChangedListener
- {
-
-
- const STATE_MANAGED = 1;
-
-
-
- const STATE_NEW = 2;
-
-
-
- const STATE_DETACHED = 3;
-
-
-
- const STATE_REMOVED = 4;
-
-
-
- private $identityMap = array();
-
-
-
- private $entityIdentifiers = array();
-
-
-
- private $originalEntityData = array();
-
-
-
- private $entityChangeSets = array();
-
-
-
- private $entityStates = array();
-
-
-
- private $scheduledForDirtyCheck = array();
-
-
-
- private $entityInsertions = array();
-
-
-
- private $entityUpdates = array();
-
-
-
- private $extraUpdates = array();
-
-
-
- private $entityDeletions = array();
-
-
-
- private $collectionDeletions = array();
-
-
-
- private $collectionUpdates = array();
-
-
-
- private $visitedCollections = array();
-
-
-
- private $em;
-
-
-
- private $commitOrderCalculator;
-
-
-
- private $persisters = array();
-
-
-
- private $collectionPersisters = array();
-
-
-
- private $evm;
-
-
-
- private $orphanRemovals = array();
-
-
-
- private $readOnlyObjects = array();
-
-
-
- private $eagerLoadingEntities = array();
-
-
-
- public function __construct(EntityManager $em)
- {
- $this->em = $em;
- $this->evm = $em->getEventManager();
- }
-
-
-
- public function commit()
- {
-
- $this->computeChangeSets();
-
- if ( ! ($this->entityInsertions ||
- $this->entityDeletions ||
- $this->entityUpdates ||
- $this->collectionUpdates ||
- $this->collectionDeletions ||
- $this->orphanRemovals)) {
- return;
- }
-
- if ($this->orphanRemovals) {
- foreach ($this->orphanRemovals as $orphan) {
- $this->remove($orphan);
- }
- }
-
-
- if ($this->evm->hasListeners(Events::onFlush)) {
- $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em));
- }
-
-
- $commitOrder = $this->getCommitOrder();
-
- $conn = $this->em->getConnection();
-
- $conn->beginTransaction();
- try {
- if ($this->entityInsertions) {
- foreach ($commitOrder as $class) {
- $this->executeInserts($class);
- }
- }
-
- if ($this->entityUpdates) {
- foreach ($commitOrder as $class) {
- $this->executeUpdates($class);
- }
- }
-
-
- if ($this->extraUpdates) {
- $this->executeExtraUpdates();
- }
-
-
- foreach ($this->collectionDeletions as $collectionToDelete) {
- $this->getCollectionPersister($collectionToDelete->getMapping())
- ->delete($collectionToDelete);
- }
-
- foreach ($this->collectionUpdates as $collectionToUpdate) {
- $this->getCollectionPersister($collectionToUpdate->getMapping())
- ->update($collectionToUpdate);
- }
-
-
- if ($this->entityDeletions) {
- for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) {
- $this->executeDeletions($commitOrder[$i]);
- }
- }
-
- $conn->commit();
- } catch (Exception $e) {
- $this->em->close();
- $conn->rollback();
- throw $e;
- }
-
-
- foreach ($this->visitedCollections as $coll) {
- $coll->takeSnapshot();
- }
-
-
- $this->entityInsertions =
- $this->entityUpdates =
- $this->entityDeletions =
- $this->extraUpdates =
- $this->entityChangeSets =
- $this->collectionUpdates =
- $this->collectionDeletions =
- $this->visitedCollections =
- $this->scheduledForDirtyCheck =
- $this->orphanRemovals = array();
- }
-
-
-
- private function executeExtraUpdates()
- {
- foreach ($this->extraUpdates as $oid => $update) {
- list ($entity, $changeset) = $update;
- $this->entityChangeSets[$oid] = $changeset;
- $this->getEntityPersister(get_class($entity))->update($entity);
- }
- }
-
-
-
- public function getEntityChangeSet($entity)
- {
- $oid = spl_object_hash($entity);
- if (isset($this->entityChangeSets[$oid])) {
- return $this->entityChangeSets[$oid];
- }
- return array();
- }
-
-
-
- public function computeChangeSet(ClassMetadata $class, $entity)
- {
- if ( ! $class->isInheritanceTypeNone()) {
- $class = $this->em->getClassMetadata(get_class($entity));
- }
-
- $oid = spl_object_hash($entity);
-
- if (isset($this->readOnlyObjects[$oid])) {
- return;
- }
-
- $actualData = array();
- foreach ($class->reflFields as $name => $refProp) {
- $value = $refProp->getValue($entity);
- if (isset($class->associationMappings[$name])
- && ($class->associationMappings[$name]['type'] & ClassMetadata::TO_MANY)
- && $value !== null
- && ! ($value instanceof PersistentCollection)) {
-
-
- if ( ! $value instanceof Collection) {
- $value = new ArrayCollection($value);
- }
-
- $assoc = $class->associationMappings[$name];
-
-
- $coll = new PersistentCollection(
- $this->em,
- $this->em->getClassMetadata($assoc['targetEntity']),
- $value
- );
-
- $coll->setOwner($entity, $assoc);
- $coll->setDirty( ! $coll->isEmpty());
- $class->reflFields[$name]->setValue($entity, $coll);
- $actualData[$name] = $coll;
- } else if ( (! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) && ($name !== $class->versionField) ) {
- $actualData[$name] = $value;
- }
- }
-
- if ( ! isset($this->originalEntityData[$oid])) {
-
-
- $this->originalEntityData[$oid] = $actualData;
- $changeSet = array();
- foreach ($actualData as $propName => $actualValue) {
- if (isset($class->associationMappings[$propName])) {
- $assoc = $class->associationMappings[$propName];
- if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
- $changeSet[$propName] = array(null, $actualValue);
- }
- } else {
- $changeSet[$propName] = array(null, $actualValue);
- }
- }
- $this->entityChangeSets[$oid] = $changeSet;
- } else {
-
-
- $originalData = $this->originalEntityData[$oid];
- $isChangeTrackingNotify = $class->isChangeTrackingNotify();
- $changeSet = ($isChangeTrackingNotify && isset($this->entityChangeSets[$oid])) ? $this->entityChangeSets[$oid] : array();
-
- foreach ($actualData as $propName => $actualValue) {
- if (isset($originalData[$propName])) {
- $orgValue = $originalData[$propName];
- } else if (array_key_exists($propName, $originalData)) {
- $orgValue = null;
- } else {
-
- continue;
- }
-
- if (isset($class->associationMappings[$propName])) {
- $assoc = $class->associationMappings[$propName];
- if ($assoc['type'] & ClassMetadata::TO_ONE && $orgValue !== $actualValue) {
- if ($assoc['isOwningSide']) {
- $changeSet[$propName] = array($orgValue, $actualValue);
- }
- if ($orgValue !== null && $assoc['orphanRemoval']) {
- $this->scheduleOrphanRemoval($orgValue);
- }
- } else if ($orgValue instanceof PersistentCollection && $orgValue !== $actualValue) {
-
- if ( ! in_array($orgValue, $this->collectionDeletions, true)) {
- $this->collectionDeletions[] = $orgValue;
- $changeSet[$propName] = $orgValue;
- }
- }
- } else if ($isChangeTrackingNotify) {
- continue;
- } else if ($orgValue !== $actualValue) {
- $changeSet[$propName] = array($orgValue, $actualValue);
- }
- }
- if ($changeSet) {
- $this->entityChangeSets[$oid] = $changeSet;
- $this->originalEntityData[$oid] = $actualData;
- $this->entityUpdates[$oid] = $entity;
- }
- }
-
-
- foreach ($class->associationMappings as $field => $assoc) {
- $val = $class->reflFields[$field]->getValue($entity);
- if ($val !== null) {
- $this->computeAssociationChanges($assoc, $val);
- }
- }
- }
-
-
-
- public function computeChangeSets()
- {
-
- foreach ($this->entityInsertions as $entity) {
- $class = $this->em->getClassMetadata(get_class($entity));
- $this->computeChangeSet($class, $entity);
- }
-
-
- foreach ($this->identityMap as $className => $entities) {
- $class = $this->em->getClassMetadata($className);
-
-
- if ($class->isReadOnly) {
- continue;
- }
-
-
-
- $entitiesToProcess = ! $class->isChangeTrackingDeferredImplicit() ?
- (isset($this->scheduledForDirtyCheck[$className]) ?
- $this->scheduledForDirtyCheck[$className] : array())
- : $entities;
-
- foreach ($entitiesToProcess as $entity) {
-
- if ($entity instanceof Proxy && ! $entity->__isInitialized__) {
- continue;
- }
-
- $oid = spl_object_hash($entity);
- if ( ! isset($this->entityInsertions[$oid]) && isset($this->entityStates[$oid])) {
- $this->computeChangeSet($class, $entity);
- }
- }
- }
- }
-
-
-
- private function computeAssociationChanges($assoc, $value)
- {
- if ($value instanceof PersistentCollection && $value->isDirty()) {
- if ($assoc['isOwningSide']) {
- $this->collectionUpdates[] = $value;
- }
- $this->visitedCollections[] = $value;
- }
-
-
-
- if ($assoc['type'] & ClassMetadata::TO_ONE) {
- if ($value instanceof Proxy && ! $value->__isInitialized__) {
- return;
- }
- $value = array($value);
- } else if ($value instanceof PersistentCollection) {
-
- $value = $value->unwrap();
- }
-
- $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
- foreach ($value as $entry) {
- $state = $this->getEntityState($entry, self::STATE_NEW);
- $oid = spl_object_hash($entry);
- if ($state == self::STATE_NEW) {
- if ( ! $assoc['isCascadePersist']) {
- throw new InvalidArgumentException("A new entity was found through the relationship '"
- . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' that was not"
- . " configured to cascade persist operations for entity: " . self::objToStr($entry) . "."
- . " Explicitly persist the new entity or configure cascading persist operations"
- . " on the relationship. If you cannot find out which entity causes the problem"
- . " implement '" . $assoc['targetEntity'] . "#__toString()' to get a clue.");
- }
- $this->persistNew($targetClass, $entry);
- $this->computeChangeSet($targetClass, $entry);
- } else if ($state == self::STATE_REMOVED) {
- return new InvalidArgumentException("Removed entity detected during flush: "
- . self::objToStr($entry).". Remove deleted entities from associations.");
- } else if ($state == self::STATE_DETACHED) {
-
-
- throw new InvalidArgumentException("A detached entity was found through a "
- . "relationship during cascading a persist operation.");
- }
-
-
- }
- }
-
- private function persistNew($class, $entity)
- {
- $oid = spl_object_hash($entity);
- if (isset($class->lifecycleCallbacks[Events::prePersist])) {
- $class->invokeLifecycleCallbacks(Events::prePersist, $entity);
- }
- if ($this->evm->hasListeners(Events::prePersist)) {
- $this->evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($entity, $this->em));
- }
-
- $idGen = $class->idGenerator;
- if ( ! $idGen->isPostInsertGenerator()) {
- $idValue = $idGen->generate($this->em, $entity);
- if ( ! $idGen instanceof \Doctrine\ORM\Id\AssignedGenerator) {
- $this->entityIdentifiers[$oid] = array($class->identifier[0] => $idValue);
- $class->setIdentifierValues($entity, $this->entityIdentifiers[$oid]);
- } else {
- $this->entityIdentifiers[$oid] = $idValue;
- }
- }
- $this->entityStates[$oid] = self::STATE_MANAGED;
-
- $this->scheduleForInsert($entity);
- }
-
-
-
- public function recomputeSingleEntityChangeSet($class, $entity)
- {
- $oid = spl_object_hash($entity);
-
- if ( ! isset($this->entityStates[$oid]) || $this->entityStates[$oid] != self::STATE_MANAGED) {
- throw new InvalidArgumentException('Entity must be managed.');
- }
-
-
-
-
- if ( ! $class->isInheritanceTypeNone()) {
- $class = $this->em->getClassMetadata(get_class($entity));
- }
-
- $actualData = array();
- foreach ($class->reflFields as $name => $refProp) {
- if ( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) {
- $actualData[$name] = $refProp->getValue($entity);
- }
- }
-
- $originalData = $this->originalEntityData[$oid];
- $changeSet = array();
-
- foreach ($actualData as $propName => $actualValue) {
- $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null;
- if (is_object($orgValue) && $orgValue !== $actualValue) {
- $changeSet[$propName] = array($orgValue, $actualValue);
- } else if ($orgValue != $actualValue || ($orgValue === null ^ $actualValue === null)) {
- $changeSet[$propName] = array($orgValue, $actualValue);
- }
- }
-
- if ($changeSet) {
- if (isset($this->entityChangeSets[$oid])) {
- $this->entityChangeSets[$oid] = array_merge($this->entityChangeSets[$oid], $changeSet);
- }
- $this->originalEntityData[$oid] = $actualData;
- }
- }
-
-
-
- private function executeInserts($class)
- {
- $className = $class->name;
- $persister = $this->getEntityPersister($className);
-
- $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postPersist]);
- $hasListeners = $this->evm->hasListeners(Events::postPersist);
- if ($hasLifecycleCallbacks || $hasListeners) {
- $entities = array();
- }
-
- foreach ($this->entityInsertions as $oid => $entity) {
- if (get_class($entity) === $className) {
- $persister->addInsert($entity);
- unset($this->entityInsertions[$oid]);
- if ($hasLifecycleCallbacks || $hasListeners) {
- $entities[] = $entity;
- }
- }
- }
-
- $postInsertIds = $persister->executeInserts();
-
- if ($postInsertIds) {
-
- foreach ($postInsertIds as $id => $entity) {
- $oid = spl_object_hash($entity);
- $idField = $class->identifier[0];
- $class->reflFields[$idField]->setValue($entity, $id);
- $this->entityIdentifiers[$oid] = array($idField => $id);
- $this->entityStates[$oid] = self::STATE_MANAGED;
- $this->originalEntityData[$oid][$idField] = $id;
- $this->addToIdentityMap($entity);
- }
- }
-
- if ($hasLifecycleCallbacks || $hasListeners) {
- foreach ($entities as $entity) {
- if ($hasLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::postPersist, $entity);
- }
- if ($hasListeners) {
- $this->evm->dispatchEvent(Events::postPersist, new LifecycleEventArgs($entity, $this->em));
- }
- }
- }
- }
-
-
-
- private function executeUpdates($class)
- {
- $className = $class->name;
- $persister = $this->getEntityPersister($className);
-
- $hasPreUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::preUpdate]);
- $hasPreUpdateListeners = $this->evm->hasListeners(Events::preUpdate);
- $hasPostUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postUpdate]);
- $hasPostUpdateListeners = $this->evm->hasListeners(Events::postUpdate);
-
- foreach ($this->entityUpdates as $oid => $entity) {
- if (get_class($entity) == $className || $entity instanceof Proxy && get_parent_class($entity) == $className) {
-
- if ($hasPreUpdateLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::preUpdate, $entity);
- $this->recomputeSingleEntityChangeSet($class, $entity);
- }
-
- if ($hasPreUpdateListeners) {
- $this->evm->dispatchEvent(Events::preUpdate, new Event\PreUpdateEventArgs(
- $entity, $this->em, $this->entityChangeSets[$oid])
- );
- }
-
- if ($this->entityChangeSets[$oid]) {
- $persister->update($entity);
- }
- unset($this->entityUpdates[$oid]);
-
- if ($hasPostUpdateLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::postUpdate, $entity);
- }
- if ($hasPostUpdateListeners) {
- $this->evm->dispatchEvent(Events::postUpdate, new LifecycleEventArgs($entity, $this->em));
- }
- }
- }
- }
-
-
-
- private function executeDeletions($class)
- {
- $className = $class->name;
- $persister = $this->getEntityPersister($className);
-
- $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postRemove]);
- $hasListeners = $this->evm->hasListeners(Events::postRemove);
-
- foreach ($this->entityDeletions as $oid => $entity) {
- if (get_class($entity) == $className || $entity instanceof Proxy && get_parent_class($entity) == $className) {
- $persister->delete($entity);
- unset(
- $this->entityDeletions[$oid],
- $this->entityIdentifiers[$oid],
- $this->originalEntityData[$oid],
- $this->entityStates[$oid]
- );
-
-
-
- if ( ! $class->isIdentifierNatural()) {
- $class->reflFields[$class->identifier[0]]->setValue($entity, null);
- }
-
- if ($hasLifecycleCallbacks) {
- $class->invokeLifecycleCallbacks(Events::postRemove, $entity);
- }
- if ($hasListeners) {
- $this->evm->dispatchEvent(Events::postRemove, new LifecycleEventArgs($entity, $this->em));
- }
- }
- }
- }
-
-
-
- private function getCommitOrder(array $entityChangeSet = null)
- {
- if ($entityChangeSet === null) {
- $entityChangeSet = array_merge(
- $this->entityInsertions,
- $this->entityUpdates,
- $this->entityDeletions
- );
- }
-
- $calc = $this->getCommitOrderCalculator();
-
-
-
-
-
- $newNodes = array();
- foreach ($entityChangeSet as $oid => $entity) {
- $className = get_class($entity);
- if ( ! $calc->hasClass($className)) {
- $class = $this->em->getClassMetadata($className);
- $calc->addClass($class);
- $newNodes[] = $class;
- }
- }
-
-
- while ($class = array_pop($newNodes)) {
- foreach ($class->associationMappings as $assoc) {
- if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) {
- $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
- if ( ! $calc->hasClass($targetClass->name)) {
- $calc->addClass($targetClass);
- $newNodes[] = $targetClass;
- }
- $calc->addDependency($targetClass, $class);
-
-
- if ($targetClass->subClasses) {
- foreach ($targetClass->subClasses as $subClassName) {
- $targetSubClass = $this->em->getClassMetadata($subClassName);
- if ( ! $calc->hasClass($subClassName)) {
- $calc->addClass($targetSubClass);
- $newNodes[] = $targetSubClass;
- }
- $calc->addDependency($targetSubClass, $class);
- }
- }
- }
- }
- }
-
- return $calc->getCommitOrder();
- }
-
-
-
- public function scheduleForInsert($entity)
- {
- $oid = spl_object_hash($entity);
-
- if (isset($this->entityUpdates[$oid])) {
- throw new InvalidArgumentException("Dirty entity can not be scheduled for insertion.");
- }
- if (isset($this->entityDeletions[$oid])) {
- throw new InvalidArgumentException("Removed entity can not be scheduled for insertion.");
- }
- if (isset($this->entityInsertions[$oid])) {
- throw new InvalidArgumentException("Entity can not be scheduled for insertion twice.");
- }
-
- $this->entityInsertions[$oid] = $entity;
-
- if (isset($this->entityIdentifiers[$oid])) {
- $this->addToIdentityMap($entity);
- }
- }
-
-
-
- public function isScheduledForInsert($entity)
- {
- return isset($this->entityInsertions[spl_object_hash($entity)]);
- }
-
-
-
- public function scheduleForUpdate($entity)
- {
- $oid = spl_object_hash($entity);
- if ( ! isset($this->entityIdentifiers[$oid])) {
- throw new InvalidArgumentException("Entity has no identity.");
- }
- if (isset($this->entityDeletions[$oid])) {
- throw new InvalidArgumentException("Entity is removed.");
- }
-
- if ( ! isset($this->entityUpdates[$oid]) && ! isset($this->entityInsertions[$oid])) {
- $this->entityUpdates[$oid] = $entity;
- }
- }
-
-
-
- public function scheduleExtraUpdate($entity, array $changeset)
- {
- $oid = spl_object_hash($entity);
- if (isset($this->extraUpdates[$oid])) {
- list($ignored, $changeset2) = $this->extraUpdates[$oid];
- $this->extraUpdates[$oid] = array($entity, $changeset + $changeset2);
- } else {
- $this->extraUpdates[$oid] = array($entity, $changeset);
- }
- }
-
-
-
- public function isScheduledForUpdate($entity)
- {
- return isset($this->entityUpdates[spl_object_hash($entity)]);
- }
-
- public function isScheduledForDirtyCheck($entity)
- {
- $rootEntityName = $this->em->getClassMetadata(get_class($entity))->rootEntityName;
- return isset($this->scheduledForDirtyCheck[$rootEntityName][spl_object_hash($entity)]);
- }
-
-
-
- public function scheduleForDelete($entity)
- {
- $oid = spl_object_hash($entity);
-
- if (isset($this->entityInsertions[$oid])) {
- if ($this->isInIdentityMap($entity)) {
- $this->removeFromIdentityMap($entity);
- }
- unset($this->entityInsertions[$oid], $this->entityStates[$oid]);
- return;
- }
-
- if ( ! $this->isInIdentityMap($entity)) {
- return;
- }
-
- $this->removeFromIdentityMap($entity);
-
- if (isset($this->entityUpdates[$oid])) {
- unset($this->entityUpdates[$oid]);
- }
- if ( ! isset($this->entityDeletions[$oid])) {
- $this->entityDeletions[$oid] = $entity;
- $this->entityStates[$oid] = self::STATE_REMOVED;
- }
- }
-
-
-
- public function isScheduledForDelete($entity)
- {
- return isset($this->entityDeletions[spl_object_hash($entity)]);
- }
-
-
-
- public function isEntityScheduled($entity)
- {
- $oid = spl_object_hash($entity);
- return isset($this->entityInsertions[$oid]) ||
- isset($this->entityUpdates[$oid]) ||
- isset($this->entityDeletions[$oid]);
- }
-
-
-
- public function addToIdentityMap($entity)
- {
- $classMetadata = $this->em->getClassMetadata(get_class($entity));
- $idHash = implode(' ', $this->entityIdentifiers[spl_object_hash($entity)]);
- if ($idHash === '') {
- throw new InvalidArgumentException("The given entity has no identity.");
- }
- $className = $classMetadata->rootEntityName;
- if (isset($this->identityMap[$className][$idHash])) {
- return false;
- }
- $this->identityMap[$className][$idHash] = $entity;
- if ($entity instanceof NotifyPropertyChanged) {
- $entity->addPropertyChangedListener($this);
- }
- return true;
- }
-
-
-
- public function getEntityState($entity, $assume = null)
- {
- $oid = spl_object_hash($entity);
- if ( ! isset($this->entityStates[$oid])) {
-
-
-
-
- if ($assume === null) {
- $class = $this->em->getClassMetadata(get_class($entity));
- $id = $class->getIdentifierValues($entity);
- if ( ! $id) {
- return self::STATE_NEW;
- } else if ($class->isIdentifierNatural()) {
-
- if ($class->isVersioned) {
- if ($class->getFieldValue($entity, $class->versionField)) {
- return self::STATE_DETACHED;
- } else {
- return self::STATE_NEW;
- }
- } else {
-
- if ($this->tryGetById($id, $class->rootEntityName)) {
- return self::STATE_DETACHED;
- } else {
-
- if ($this->getEntityPersister(get_class($entity))->exists($entity)) {
- return self::STATE_DETACHED;
- } else {
- return self::STATE_NEW;
- }
- }
- }
- } else if (!$class->idGenerator->isPostInsertGenerator()) {
-
-
-
-
-
- if ($this->tryGetById($id, $class->rootEntityName)) {
- return self::STATE_DETACHED;
- } else {
-
- if ($this->getEntityPersister(get_class($entity))->exists($entity)) {
- return self::STATE_DETACHED;
- } else {
- return self::STATE_NEW;
- }
- }
- } else {
- return self::STATE_DETACHED;
- }
- } else {
- return $assume;
- }
- }
- return $this->entityStates[$oid];
- }
-
-
-
- public function removeFromIdentityMap($entity)
- {
- $oid = spl_object_hash($entity);
- $classMetadata = $this->em->getClassMetadata(get_class($entity));
- $idHash = implode(' ', $this->entityIdentifiers[$oid]);
- if ($idHash === '') {
- throw new InvalidArgumentException("The given entity has no identity.");
- }
- $className = $classMetadata->rootEntityName;
- if (isset($this->identityMap[$className][$idHash])) {
- unset($this->identityMap[$className][$idHash]);
-
- return true;
- }
-
- return false;
- }
-
-
-
- public function getByIdHash($idHash, $rootClassName)
- {
- return $this->identityMap[$rootClassName][$idHash];
- }
-
-
-
- public function tryGetByIdHash($idHash, $rootClassName)
- {
- return isset($this->identityMap[$rootClassName][$idHash]) ?
- $this->identityMap[$rootClassName][$idHash] : false;
- }
-
-
-
- public function isInIdentityMap($entity)
- {
- $oid = spl_object_hash($entity);
- if ( ! isset($this->entityIdentifiers[$oid])) {
- return false;
- }
- $classMetadata = $this->em->getClassMetadata(get_class($entity));
- $idHash = implode(' ', $this->entityIdentifiers[$oid]);
- if ($idHash === '') {
- return false;
- }
-
- return isset($this->identityMap[$classMetadata->rootEntityName][$idHash]);
- }
-
-
-
- public function containsIdHash($idHash, $rootClassName)
- {
- return isset($this->identityMap[$rootClassName][$idHash]);
- }
-
-
-
- public function persist($entity)
- {
- $visited = array();
- $this->doPersist($entity, $visited);
- }
-
-
-
- private function doPersist($entity, array &$visited)
- {
- $oid = spl_object_hash($entity);
- if (isset($visited[$oid])) {
- return;
- }
-
- $visited[$oid] = $entity;
-
- $class = $this->em->getClassMetadata(get_class($entity));
-
-
-
-
-
- $entityState = $this->getEntityState($entity, self::STATE_NEW);
-
- switch ($entityState) {
- case self::STATE_MANAGED:
-
- if ($class->isChangeTrackingDeferredExplicit()) {
- $this->scheduleForDirtyCheck($entity);
- }
- break;
- case self::STATE_NEW:
- $this->persistNew($class, $entity);
- break;
- case self::STATE_REMOVED:
-
- unset($this->entityDeletions[$oid]);
- $this->entityStates[$oid] = self::STATE_MANAGED;
- break;
- case self::STATE_DETACHED:
-
- throw new InvalidArgumentException("Detached entity passed to persist().");
- default:
- throw new UnexpectedValueException("Unexpected entity state: $entityState.");
- }
-
- $this->cascadePersist($entity, $visited);
- }
-
-
-
- public function remove($entity)
- {
- $visited = array();
- $this->doRemove($entity, $visited);
- }
-
-
-
- private function doRemove($entity, array &$visited)
- {
- $oid = spl_object_hash($entity);
- if (isset($visited[$oid])) {
- return;
- }
-
- $visited[$oid] = $entity;
-
-
-
- $this->cascadeRemove($entity, $visited);
-
- $class = $this->em->getClassMetadata(get_class($entity));
- $entityState = $this->getEntityState($entity);
- switch ($entityState) {
- case self::STATE_NEW:
- case self::STATE_REMOVED:
-
- break;
- case self::STATE_MANAGED:
- if (isset($class->lifecycleCallbacks[Events::preRemove])) {
- $class->invokeLifecycleCallbacks(Events::preRemove, $entity);
- }
- if ($this->evm->hasListeners(Events::preRemove)) {
- $this->evm->dispatchEvent(Events::preRemove, new LifecycleEventArgs($entity, $this->em));
- }
- $this->scheduleForDelete($entity);
- break;
- case self::STATE_DETACHED:
- throw new InvalidArgumentException("A detached entity can not be removed.");
- default:
- throw new UnexpectedValueException("Unexpected entity state: $entityState.");
- }
-
- }
-
-
-
- public function merge($entity)
- {
- $visited = array();
- return $this->doMerge($entity, $visited);
- }
-
-
-
- private function doMerge($entity, array &$visited, $prevManagedCopy = null, $assoc = null)
- {
- $oid = spl_object_hash($entity);
- if (isset($visited[$oid])) {
- return;
- }
-
- $visited[$oid] = $entity;
-
- $class = $this->em->getClassMetadata(get_class($entity));
-
-
-
-
-
- if ($this->getEntityState($entity, self::STATE_DETACHED) == self::STATE_MANAGED) {
- $managedCopy = $entity;
- } else {
- if ($entity instanceof Proxy && ! $entity->__isInitialized__) {
- $entity->__load();
- }
-
-
- $id = $class->getIdentifierValues($entity);
-
-
- if ( ! $id) {
- $managedCopy = $class->newInstance();
- $this->persistNew($class, $managedCopy);
- } else {
- $flatId = $id;
- if ($class->containsForeignIdentifier) {
-
-
- foreach ($id as $idField => $idValue) {
- if (isset($class->associationMappings[$idField])) {
- $targetClassMetadata = $this->em->getClassMetadata($class->associationMappings[$idField]['targetEntity']);
- $associatedId = $this->getEntityIdentifier($idValue);
- $flatId[$idField] = $associatedId[$targetClassMetadata->identifier[0]];
- }
- }
- }
-
- $managedCopy = $this->tryGetById($flatId, $class->rootEntityName);
- if ($managedCopy) {
-
- if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) {
- throw new InvalidArgumentException('Removed entity detected during merge.'
- . ' Can not merge with a removed entity.');
- }
- } else {
-
- $managedCopy = $this->em->find($class->name, $flatId);
- }
-
- if ($managedCopy === null) {
-
-
- if ($class->isIdentifierNatural()) {
- $managedCopy = $class->newInstance();
- $class->setIdentifierValues($managedCopy, $id);
- $this->persistNew($class, $managedCopy);
- } else {
- throw new EntityNotFoundException;
- }
-
- $managedCopy = $this->newInstance($class);
- $class->setIdentifierValues($managedCopy, $id);
-
- $this->persistNew($class, $managedCopy);
- } else {
- if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized__) {
- $managedCopy->__load();
- }
- }
- }
-
- if ($class->isVersioned) {
- $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy);
- $entityVersion = $class->reflFields[$class->versionField]->getValue($entity);
-
- if ($managedCopyVersion != $entityVersion) {
- throw OptimisticLockException::lockFailedVersionMissmatch($entity, $entityVersion, $managedCopyVersion);
- }
- }
-
-
- foreach ($class->reflFields as $name => $prop) {
- if ( ! isset($class->associationMappings[$name])) {
- if ( ! $class->isIdentifier($name)) {
- $prop->setValue($managedCopy, $prop->getValue($entity));
- }
- } else {
- $assoc2 = $class->associationMappings[$name];
- if ($assoc2['type'] & ClassMetadata::TO_ONE) {
- $other = $prop->getValue($entity);
- if ($other === null) {
- $prop->setValue($managedCopy, null);
- } else if ($other instanceof Proxy && !$other->__isInitialized__) {
-
- continue;
- } else if ( ! $assoc2['isCascadeMerge']) {
- if ($this->getEntityState($other, self::STATE_DETACHED) !== self::STATE_MANAGED) {
- $targetClass = $this->em->getClassMetadata($assoc2['targetEntity']);
- $relatedId = $targetClass->getIdentifierValues($other);
-
- if ($targetClass->subClasses) {
- $other = $this->em->find($targetClass->name, $relatedId);
- } else {
- $other = $this->em->getProxyFactory()->getProxy($assoc2['targetEntity'], $relatedId);
- $this->registerManaged($other, $relatedId, array());
- }
- }
- $prop->setValue($managedCopy, $other);
- }
- } else {
- $mergeCol = $prop->getValue($entity);
- if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized()) {
-
-
- continue;
- }
-
- $managedCol = $prop->getValue($managedCopy);
- if (!$managedCol) {
- $managedCol = new PersistentCollection($this->em,
- $this->em->getClassMetadata($assoc2['targetEntity']),
- new ArrayCollection
- );
- $managedCol->setOwner($managedCopy, $assoc2);
- $prop->setValue($managedCopy, $managedCol);
- $this->originalEntityData[$oid][$name] = $managedCol;
- }
- if ($assoc2['isCascadeMerge']) {
- $managedCol->initialize();
-
- if (!$managedCol->isEmpty() && $managedCol != $mergeCol) {
- $managedCol->unwrap()->clear();
- $managedCol->setDirty(true);
- if ($assoc2['isOwningSide'] && $assoc2['type'] == ClassMetadata::MANY_TO_MANY && $class->isChangeTrackingNotify()) {
- $this->scheduleForDirtyCheck($managedCopy);
- }
- }
- }
- }
- }
- if ($class->isChangeTrackingNotify()) {
-
- $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy));
- }
- }
- if ($class->isChangeTrackingDeferredExplicit()) {
- $this->scheduleForDirtyCheck($entity);
- }
- }
-
- if ($prevManagedCopy !== null) {
- $assocField = $assoc['fieldName'];
- $prevClass = $this->em->getClassMetadata(get_class($prevManagedCopy));
- if ($assoc['type'] & ClassMetadata::TO_ONE) {
- $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy);
- } else {
- $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy);
- if ($assoc['type'] == ClassMetadata::ONE_TO_MANY) {
- $class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy);
- }
- }
- }
-
-
- $visited[spl_object_hash($managedCopy)] = true;
-
- $this->cascadeMerge($entity, $managedCopy, $visited);
-
- return $managedCopy;
- }
-
-
-
- public function detach($entity)
- {
- $visited = array();
- $this->doDetach($entity, $visited);
- }
-
-
-
- private function doDetach($entity, array &$visited)
- {
- $oid = spl_object_hash($entity);
- if (isset($visited[$oid])) {
- return;
- }
-
- $visited[$oid] = $entity;
-
- switch ($this->getEntityState($entity, self::STATE_DETACHED)) {
- case self::STATE_MANAGED:
- if ($this->isInIdentityMap($entity)) {
- $this->removeFromIdentityMap($entity);
- }
- unset($this->entityInsertions[$oid], $this->entityUpdates[$oid],
- $this->entityDeletions[$oid], $this->entityIdentifiers[$oid],
- $this->entityStates[$oid], $this->originalEntityData[$oid]);
- break;
- case self::STATE_NEW:
- case self::STATE_DETACHED:
- return;
- }
-
- $this->cascadeDetach($entity, $visited);
- }
-
-
-
- public function refresh($entity)
- {
- $visited = array();
- $this->doRefresh($entity, $visited);
- }
-
-
-
- private function doRefresh($entity, array &$visited)
- {
- $oid = spl_object_hash($entity);
- if (isset($visited[$oid])) {
- return;
- }
-
- $visited[$oid] = $entity;
-
- $class = $this->em->getClassMetadata(get_class($entity));
- if ($this->getEntityState($entity) == self::STATE_MANAGED) {
- $this->getEntityPersister($class->name)->refresh(
- array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
- $entity
- );
- } else {
- throw new InvalidArgumentException("Entity is not MANAGED.");
- }
-
- $this->cascadeRefresh($entity, $visited);
- }
-
-
-
- private function cascadeRefresh($entity, array &$visited)
- {
- $class = $this->em->getClassMetadata(get_class($entity));
- foreach ($class->associationMappings as $assoc) {
- if ( ! $assoc['isCascadeRefresh']) {
- continue;
- }
- $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
- if ($relatedEntities instanceof Collection) {
- if ($relatedEntities instanceof PersistentCollection) {
-
- $relatedEntities = $relatedEntities->unwrap();
- }
- foreach ($relatedEntities as $relatedEntity) {
- $this->doRefresh($relatedEntity, $visited);
- }
- } else if ($relatedEntities !== null) {
- $this->doRefresh($relatedEntities, $visited);
- }
- }
- }
-
-
-
- private function cascadeDetach($entity, array &$visited)
- {
- $class = $this->em->getClassMetadata(get_class($entity));
- foreach ($class->associationMappings as $assoc) {
- if ( ! $assoc['isCascadeDetach']) {
- continue;
- }
- $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
- if ($relatedEntities instanceof Collection) {
- if ($relatedEntities instanceof PersistentCollection) {
-
- $relatedEntities = $relatedEntities->unwrap();
- }
- foreach ($relatedEntities as $relatedEntity) {
- $this->doDetach($relatedEntity, $visited);
- }
- } else if ($relatedEntities !== null) {
- $this->doDetach($relatedEntities, $visited);
- }
- }
- }
-
-
-
- private function cascadeMerge($entity, $managedCopy, array &$visited)
- {
- $class = $this->em->getClassMetadata(get_class($entity));
- foreach ($class->associationMappings as $assoc) {
- if ( ! $assoc['isCascadeMerge']) {
- continue;
- }
- $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
- if ($relatedEntities instanceof Collection) {
- if ($relatedEntities === $class->reflFields[$assoc['fieldName']]->getValue($managedCopy)) {
- continue;
- }
-
- if ($relatedEntities instanceof PersistentCollection) {
-
- $relatedEntities = $relatedEntities->unwrap();
- }
- foreach ($relatedEntities as $relatedEntity) {
- $this->doMerge($relatedEntity, $visited, $managedCopy, $assoc);
- }
- } else if ($relatedEntities !== null) {
- $this->doMerge($relatedEntities, $visited, $managedCopy, $assoc);
- }
- }
- }
-
-
-
- private function cascadePersist($entity, array &$visited)
- {
- $class = $this->em->getClassMetadata(get_class($entity));
- foreach ($class->associationMappings as $assoc) {
- if ( ! $assoc['isCascadePersist']) {
- continue;
- }
-
- $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
- if (($relatedEntities instanceof Collection || is_array($relatedEntities))) {
- if ($relatedEntities instanceof PersistentCollection) {
-
- $relatedEntities = $relatedEntities->unwrap();
- }
- foreach ($relatedEntities as $relatedEntity) {
- $this->doPersist($relatedEntity, $visited);
- }
- } else if ($relatedEntities !== null) {
- $this->doPersist($relatedEntities, $visited);
- }
- }
- }
-
-
-
- private function cascadeRemove($entity, array &$visited)
- {
- $class = $this->em->getClassMetadata(get_class($entity));
- foreach ($class->associationMappings as $assoc) {
- if ( ! $assoc['isCascadeRemove']) {
- continue;
- }
-
- if ($entity instanceof Proxy && !$entity->__isInitialized__) {
- $entity->__load();
- }
-
- $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity);
- if ($relatedEntities instanceof Collection || is_array($relatedEntities)) {
-
- foreach ($relatedEntities as $relatedEntity) {
- $this->doRemove($relatedEntity, $visited);
- }
- } else if ($relatedEntities !== null) {
- $this->doRemove($relatedEntities, $visited);
- }
- }
- }
-
-
-
- public function lock($entity, $lockMode, $lockVersion = null)
- {
- if ($this->getEntityState($entity, self::STATE_DETACHED) != self::STATE_MANAGED) {
- throw new InvalidArgumentException("Entity is not MANAGED.");
- }
-
- $entityName = get_class($entity);
- $class = $this->em->getClassMetadata($entityName);
-
- if ($lockMode == \Doctrine\DBAL\LockMode::OPTIMISTIC) {
- if (!$class->isVersioned) {
- throw OptimisticLockException::notVersioned($entityName);
- }
-
- if ($lockVersion != null) {
- $entityVersion = $class->reflFields[$class->versionField]->getValue($entity);
- if ($entityVersion != $lockVersion) {
- throw OptimisticLockException::lockFailedVersionMissmatch($entity, $lockVersion, $entityVersion);
- }
- }
- } else if (in_array($lockMode, array(\Doctrine\DBAL\LockMode::PESSIMISTIC_READ, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE))) {
-
- if (!$this->em->getConnection()->isTransactionActive()) {
- throw TransactionRequiredException::transactionRequired();
- }
-
- $oid = spl_object_hash($entity);
-
- $this->getEntityPersister($class->name)->lock(
- array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
- $lockMode
- );
- }
- }
-
-
-
- public function getCommitOrderCalculator()
- {
- if ($this->commitOrderCalculator === null) {
- $this->commitOrderCalculator = new Internal\CommitOrderCalculator;
- }
- return $this->commitOrderCalculator;
- }
-
-
-
- public function clear()
- {
- $this->identityMap =
- $this->entityIdentifiers =
- $this->originalEntityData =
- $this->entityChangeSets =
- $this->entityStates =
- $this->scheduledForDirtyCheck =
- $this->entityInsertions =
- $this->entityUpdates =
- $this->entityDeletions =
- $this->collectionDeletions =
- $this->collectionUpdates =
- $this->extraUpdates =
- $this->orphanRemovals = array();
- if ($this->commitOrderCalculator !== null) {
- $this->commitOrderCalculator->clear();
- }
-
- if ($this->evm->hasListeners(Events::onClear)) {
- $this->evm->dispatchEvent(Events::onClear, new Event\OnClearEventArgs($this->em));
- }
- }
-
-
-
- public function scheduleOrphanRemoval($entity)
- {
- $this->orphanRemovals[spl_object_hash($entity)] = $entity;
- }
-
-
-
- public function scheduleCollectionDeletion(PersistentCollection $coll)
- {
-
-
- $this->collectionDeletions[] = $coll;
- }
-
- public function isCollectionScheduledForDeletion(PersistentCollection $coll)
- {
- return in_array($coll, $this->collectionsDeletions, true);
- }
-
-
-
- public function createEntity($className, array $data, &$hints = array())
- {
- $class = $this->em->getClassMetadata($className);
-
-
- if ($class->isIdentifierComposite) {
- $id = array();
- foreach ($class->identifier as $fieldName) {
- if (isset($class->associationMappings[$fieldName])) {
- $id[$fieldName] = $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']];
- } else {
- $id[$fieldName] = $data[$fieldName];
- }
- }
- $idHash = implode(' ', $id);
- } else {
- if (isset($class->associationMappings[$class->identifier[0]])) {
- $idHash = $data[$class->associationMappings[$class->identifier[0]]['joinColumns'][0]['name']];
- } else {
- $idHash = $data[$class->identifier[0]];
- }
- $id = array($class->identifier[0] => $idHash);
- }
-
- if (isset($this->identityMap[$class->rootEntityName][$idHash])) {
- $entity = $this->identityMap[$class->rootEntityName][$idHash];
- $oid = spl_object_hash($entity);
- if ($entity instanceof Proxy && ! $entity->__isInitialized__) {
- $entity->__isInitialized__ = true;
- $overrideLocalValues = true;
- if ($entity instanceof NotifyPropertyChanged) {
- $entity->addPropertyChangedListener($this);
- }
- } else {
- $overrideLocalValues = isset($hints[Query::HINT_REFRESH]);
-
-
- if(isset($hints[Query::HINT_REFRESH_ENTITY])) {
- $overrideLocalValues = $hints[Query::HINT_REFRESH_ENTITY] === $entity;
- }
- }
-
- if ($overrideLocalValues) {
- $this->originalEntityData[$oid] = $data;
- }
- } else {
- $entity = $class->newInstance();
- $oid = spl_object_hash($entity);
- $this->entityIdentifiers[$oid] = $id;
- $this->entityStates[$oid] = self::STATE_MANAGED;
- $this->originalEntityData[$oid] = $data;
- $this->identityMap[$class->rootEntityName][$idHash] = $entity;
- if ($entity instanceof NotifyPropertyChanged) {
- $entity->addPropertyChangedListener($this);
- }
- $overrideLocalValues = true;
- }
-
- if ($overrideLocalValues) {
- foreach ($data as $field => $value) {
- if (isset($class->fieldMappings[$field])) {
- $class->reflFields[$field]->setValue($entity, $value);
- }
- }
-
-
- unset($this->eagerLoadingEntities[$class->rootEntityName][$idHash]);
-
-
- if ( ! isset($hints[Query::HINT_FORCE_PARTIAL_LOAD])) {
- foreach ($class->associationMappings as $field => $assoc) {
-
- if (isset($hints['fetchAlias']) && isset($hints['fetched'][$hints['fetchAlias']][$field])) {
- continue;
- }
-
- $targetClass = $this->em->getClassMetadata($assoc['targetEntity']);
-
- if ($assoc['type'] & ClassMetadata::TO_ONE) {
- if ($assoc['isOwningSide']) {
- $associatedId = array();
-
- foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) {
- $joinColumnValue = isset($data[$srcColumn]) ? $data[$srcColumn] : null;
- if ($joinColumnValue !== null) {
- if ($targetClass->containsForeignIdentifier) {
- $associatedId[$targetClass->getFieldForColumn($targetColumn)] = $joinColumnValue;
- } else {
- $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue;
- }
- }
- }
- if ( ! $associatedId) {
-
- $class->reflFields[$field]->setValue($entity, null);
- $this->originalEntityData[$oid][$field] = null;
- } else {
- if (!isset($hints['fetchMode'][$class->name][$field])) {
- $hints['fetchMode'][$class->name][$field] = $assoc['fetch'];
- }
-
-
-
-
-
- $relatedIdHash = implode(' ', $associatedId);
- if (isset($this->identityMap[$targetClass->rootEntityName][$relatedIdHash])) {
- $newValue = $this->identityMap[$targetClass->rootEntityName][$relatedIdHash];
-
-
-
-
- if ($hints['fetchMode'][$class->name][$field] == ClassMetadata::FETCH_EAGER &&
- isset($hints['deferEagerLoad']) &&
- !$targetClass->isIdentifierComposite &&
- $newValue instanceof Proxy &&
- $newValue->__isInitialized__ === false) {
-
- $this->eagerLoadingEntities[$targetClass->rootEntityName][$relatedIdHash] = current($associatedId);
- }
- } else {
- if ($targetClass->subClasses) {
-
-
-
- $newValue = $this->getEntityPersister($assoc['targetEntity'])
- ->loadOneToOneEntity($assoc, $entity, $associatedId);
- } else {
-
-
- if ($hints['fetchMode'][$class->name][$field] == ClassMetadata::FETCH_EAGER) {
- if (isset($hints['deferEagerLoad']) && !$targetClass->isIdentifierComposite) {
-
- $this->eagerLoadingEntities[$targetClass->rootEntityName][$relatedIdHash] = current($associatedId);
-
- $newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId);
- } else {
-
- $newValue = $this->em->find($assoc['targetEntity'], $associatedId);
- }
- } else {
- $newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId);
- }
-
- $newValueOid = spl_object_hash($newValue);
- $this->entityIdentifiers[$newValueOid] = $associatedId;
- $this->identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue;
- $this->entityStates[$newValueOid] = self::STATE_MANAGED;
-
- }
- }
- $this->originalEntityData[$oid][$field] = $newValue;
- $class->reflFields[$field]->setValue($entity, $newValue);
-
- if ($assoc['inversedBy'] && $assoc['type'] & ClassMetadata::ONE_TO_ONE) {
- $inverseAssoc = $targetClass->associationMappings[$assoc['inversedBy']];
- $targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($newValue, $entity);
- }
- }
- } else {
-
- $class->reflFields[$field]->setValue($entity, $this->getEntityPersister($assoc['targetEntity'])
- ->loadOneToOneEntity($assoc, $entity));
- }
- } else {
-
- $pColl = new PersistentCollection($this->em, $targetClass, new ArrayCollection);
- $pColl->setOwner($entity, $assoc);
-
- $reflField = $class->reflFields[$field];
- $reflField->setValue($entity, $pColl);
-
- if ($assoc['fetch'] == ClassMetadata::FETCH_EAGER) {
- $this->loadCollection($pColl);
- $pColl->takeSnapshot();
- } else {
- $pColl->setInitialized(false);
- }
- $this->originalEntityData[$oid][$field] = $pColl;
- }
- }
- }
- }
-
-
- if (isset($class->lifecycleCallbacks[Events::postLoad])) {
- $class->invokeLifecycleCallbacks(Events::postLoad, $entity);
- }
- if ($this->evm->hasListeners(Events::postLoad)) {
- $this->evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->em));
- }
-
- return $entity;
- }
-
-
-
- public function triggerEagerLoads()
- {
- if (!$this->eagerLoadingEntities) {
- return;
- }
-
-
- $eagerLoadingEntities = $this->eagerLoadingEntities;
- $this->eagerLoadingEntities = array();
-
- foreach ($eagerLoadingEntities AS $entityName => $ids) {
- $class = $this->em->getClassMetadata($entityName);
- if ($ids) {
- $this->getEntityPersister($entityName)->loadAll(
- array_combine($class->identifier, array(array_values($ids)))
- );
- }
- }
- }
-
-
-
- public function loadCollection(PersistentCollection $collection)
- {
- $assoc = $collection->getMapping();
- switch ($assoc['type']) {
- case ClassMetadata::ONE_TO_MANY:
- $this->getEntityPersister($assoc['targetEntity'])->loadOneToManyCollection(
- $assoc, $collection->getOwner(), $collection);
- break;
- case ClassMetadata::MANY_TO_MANY:
- $this->getEntityPersister($assoc['targetEntity'])->loadManyToManyCollection(
- $assoc, $collection->getOwner(), $collection);
- break;
- }
- }
-
-
-
- public function getIdentityMap()
- {
- return $this->identityMap;
- }
-
-
-
- public function getOriginalEntityData($entity)
- {
- $oid = spl_object_hash($entity);
- if (isset($this->originalEntityData[$oid])) {
- return $this->originalEntityData[$oid];
- }
- return array();
- }
-
-
-
- public function setOriginalEntityData($entity, array $data)
- {
- $this->originalEntityData[spl_object_hash($entity)] = $data;
- }
-
-
-
- public function setOriginalEntityProperty($oid, $property, $value)
- {
- $this->originalEntityData[$oid][$property] = $value;
- }
-
-
-
- public function getEntityIdentifier($entity)
- {
- return $this->entityIdentifiers[spl_object_hash($entity)];
- }
-
-
-
- public function tryGetById($id, $rootClassName)
- {
- $idHash = implode(' ', (array) $id);
- if (isset($this->identityMap[$rootClassName][$idHash])) {
- return $this->identityMap[$rootClassName][$idHash];
- }
- return false;
- }
-
-
-
- public function scheduleForDirtyCheck($entity)
- {
- $rootClassName = $this->em->getClassMetadata(get_class($entity))->rootEntityName;
- $this->scheduledForDirtyCheck[$rootClassName][spl_object_hash($entity)] = $entity;
- }
-
-
-
- public function hasPendingInsertions()
- {
- return ! empty($this->entityInsertions);
- }
-
-
-
- public function size()
- {
- $count = 0;
- foreach ($this->identityMap as $entitySet) {
- $count += count($entitySet);
- }
- return $count;
- }
-
-
-
- public function getEntityPersister($entityName)
- {
- if ( ! isset($this->persisters[$entityName])) {
- $class = $this->em->getClassMetadata($entityName);
- if ($class->isInheritanceTypeNone()) {
- $persister = new Persisters\BasicEntityPersister($this->em, $class);
- } else if ($class->isInheritanceTypeSingleTable()) {
- $persister = new Persisters\SingleTablePersister($this->em, $class);
- } else if ($class->isInheritanceTypeJoined()) {
- $persister = new Persisters\JoinedSubclassPersister($this->em, $class);
- } else {
- $persister = new Persisters\UnionSubclassPersister($this->em, $class);
- }
- $this->persisters[$entityName] = $persister;
- }
- return $this->persisters[$entityName];
- }
-
-
-
- public function getCollectionPersister(array $association)
- {
- $type = $association['type'];
- if ( ! isset($this->collectionPersisters[$type])) {
- if ($type == ClassMetadata::ONE_TO_MANY) {
- $persister = new Persisters\OneToManyPersister($this->em);
- } else if ($type == ClassMetadata::MANY_TO_MANY) {
- $persister = new Persisters\ManyToManyPersister($this->em);
- }
- $this->collectionPersisters[$type] = $persister;
- }
- return $this->collectionPersisters[$type];
- }
-
-
-
- public function registerManaged($entity, array $id, array $data)
- {
- $oid = spl_object_hash($entity);
- $this->entityIdentifiers[$oid] = $id;
- $this->entityStates[$oid] = self::STATE_MANAGED;
- $this->originalEntityData[$oid] = $data;
- $this->addToIdentityMap($entity);
- }
-
-
-
- public function clearEntityChangeSet($oid)
- {
- $this->entityChangeSets[$oid] = array();
- }
-
-
-
-
-
- public function propertyChanged($entity, $propertyName, $oldValue, $newValue)
- {
- $oid = spl_object_hash($entity);
- $class = $this->em->getClassMetadata(get_class($entity));
-
- $isAssocField = isset($class->associationMappings[$propertyName]);
-
- if ( ! $isAssocField && ! isset($class->fieldMappings[$propertyName])) {
- return;
- }
-
-
- $this->entityChangeSets[$oid][$propertyName] = array($oldValue, $newValue);
- if ( ! isset($this->scheduledForDirtyCheck[$class->rootEntityName][$oid])) {
- $this->scheduleForDirtyCheck($entity);
- }
- }
-
-
-
- public function getScheduledEntityInsertions()
- {
- return $this->entityInsertions;
- }
-
-
-
- public function getScheduledEntityUpdates()
- {
- return $this->entityUpdates;
- }
-
-
-
- public function getScheduledEntityDeletions()
- {
- return $this->entityDeletions;
- }
-
-
-
- public function getScheduledCollectionDeletions()
- {
- return $this->collectionDeletions;
- }
-
-
-
- public function getScheduledCollectionUpdates()
- {
- return $this->collectionUpdates;
- }
-
-
-
- public function initializeObject($obj)
- {
- if ($obj instanceof Proxy) {
- $obj->__load();
- } else if ($obj instanceof PersistentCollection) {
- $obj->initialize();
- }
- }
-
-
-
- private static function objToStr($obj)
- {
- return method_exists($obj, '__toString') ? (string)$obj : get_class($obj).'@'.spl_object_hash($obj);
- }
-
-
-
- public function markReadOnly($object)
- {
- if ( ! is_object($object) || ! $this->isInIdentityMap($object)) {
- throw new InvalidArgumentException("Managed entity required");
- }
- $this->readOnlyObjects[spl_object_hash($object)] = true;
- }
-
-
-
- public function isReadOnly($object)
- {
- if ( ! is_object($object) ) {
- throw new InvalidArgumentException("Managed entity required");
- }
- return isset($this->readOnlyObjects[spl_object_hash($object)]);
- }
- }
|