1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386 |
- <?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 $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);
- $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) {
- $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null;
- 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;
- }
- }
-
-
- foreach ($newNodes as $class) {
- 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);
- }
- $calc->addDependency($targetClass, $class);
-
-
- if ($targetClass->subClasses) {
- foreach ($targetClass->subClasses as $subClassName) {
- $targetSubClass = $this->em->getClassMetadata($subClassName);
- if ( ! $calc->hasClass($subClassName)) {
- $calc->addClass($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 {
- 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 {
-
- $id = $class->getIdentifierValues($entity);
-
-
- if ( ! $id) {
- $managedCopy = $class->newInstance();
- $this->persistNew($class, $managedCopy);
- } else {
- $managedCopy = $this->tryGetById($id, $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, $id);
- }
-
- if ($managedCopy === null) {
-
-
- if ($class->isIdentifierNatural()) {
- $managedCopy = $class->newInstance();
- $class->setIdentifierValues($managedCopy, $id);
- $this->persistNew($class, $managedCopy);
- } else {
- throw new EntityNotFoundException;
- }
- }
- }
-
- 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) {
- $prop->setValue($managedCopy, $other);
- } else {
- $targetClass = $this->em->getClassMetadata($assoc2['targetEntity']);
- $id = $targetClass->getIdentifierValues($other);
- $proxy = $this->em->getProxyFactory()->getProxy($assoc2['targetEntity'], $id);
- $prop->setValue($managedCopy, $proxy);
- $this->registerManaged($proxy, $id, array());
- }
- }
- } 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_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 ($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['fetched'][$className][$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);
- $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);
- }
- }
|