Author: Elliot Levin (elliot@aanet.com.au)
Beispiel #1
0
 public function GetDiscardedRelationship(Object\Domain $Domain, Object\UnitOfWork $UnitOfWork, $ParentEntity, $RelatedEntity)
 {
     if ($this->CascadeDiscard) {
         $UnitOfWork->Discard($RelatedEntity);
     }
     return $Domain->DiscardedRelationship($ParentEntity, $RelatedEntity);
 }
 protected function PersistRelationshipChanges(Object\Domain $Domain, Object\UnitOfWork $UnitOfWork, $ParentEntity, $CurrentValue, $HasOriginalValue, $OriginalValue)
 {
     $RelationshipChanges = [];
     $OriginalEntities = [];
     $CurrentEntities = [];
     if (!$CurrentValue instanceof Collections\ICollection) {
         if (!$CurrentValue instanceof \Traversable) {
             throw new Object\ObjectException('Invalid value for collection property on entity %s, Traversable expected, %s given', $this->GetEntityMap()->GetEntityType(), \Storm\Core\Utilities::GetTypeOrClass($CurrentValue));
         }
         foreach ($CurrentValue as $Entity) {
             if ($this->IsValidEntity($Entity)) {
                 $CurrentEntities[] = $Entity;
             }
         }
     } else {
         if ($CurrentValue instanceof Collections\LazyCollection && !$CurrentValue->__IsLoaded()) {
             return [];
         } else {
             if (!$CurrentValue->__IsAltered()) {
                 foreach ($CurrentValue->ToArray() as $Entity) {
                     if ($Entity instanceof Proxies\IProxy && !$Entity->__IsAltered()) {
                         continue;
                     }
                     $UnitOfWork->PersistRelationships($Entity);
                 }
                 return [];
             } else {
                 $CurrentEntities = $CurrentValue->ToArray();
             }
         }
     }
     if ($HasOriginalValue) {
         $OriginalEntities = $OriginalValue->ToArray();
     }
     $NewOrAlteredEntities = $this->ComputeDifference($CurrentEntities, $OriginalEntities);
     $RemovedEntities = $this->ComputeIdentityDifference($Domain, $OriginalEntities, $CurrentEntities);
     foreach ($NewOrAlteredEntities as $NewEntity) {
         $RelationshipChanges[] = new Object\RelationshipChange($this->RelationshipType->GetPersistedRelationship($Domain, $UnitOfWork, $ParentEntity, $NewEntity), null);
     }
     foreach ($RemovedEntities as $RemovedEntity) {
         $RelationshipChanges[] = new Object\RelationshipChange(null, $this->RelationshipType->GetDiscardedRelationship($Domain, $UnitOfWork, $ParentEntity, $RemovedEntity));
     }
     return $RelationshipChanges;
 }
Beispiel #3
0
 public function Discard(Object\UnitOfWork $UnitOfWork, $ParentEntity)
 {
     $Domain = $UnitOfWork->GetDomain();
     list($CurrentValue, $HasOriginalValue, $OriginalValue) = $this->GetEntityRelationshipData($ParentEntity);
     $OriginalIsValidEntity = $this->IsValidEntity($OriginalValue);
     $CurrentIsValidEntity = $this->IsValidEntity($CurrentValue);
     $DiscardedRelationship = null;
     if (!$CurrentIsValidEntity && !$this->IsOptional) {
         throw $this->InvalidEntityAndIsRequired($CurrentValue);
     }
     if ($OriginalIsValidEntity) {
         $DiscardedRelationship = $this->RelationshipType->GetDiscardedRelationship($Domain, $UnitOfWork, $ParentEntity, $OriginalValue);
     }
     return new Object\RelationshipChange(null, $DiscardedRelationship);
 }
 /**
  * @access private
  * 
  * Maps a unit of work instance to the transactional equivalent.
  * 
  * @param Object\UnitOfWork $UnitOfWork The unit of work to map
  * @param Relational\Transaction $Transaction The transaction to map to
  * @return void
  */
 private function MapUnitOfWorkToTransaction(Object\UnitOfWork $UnitOfWork, Relational\Transaction $Transaction)
 {
     foreach ($UnitOfWork->GetPersistenceDataGroups() as $EntityType => $PersistenceDataGroup) {
         $this->MapPersistenceDataToTransaction($UnitOfWork, $Transaction, $PersistenceDataGroup);
     }
     foreach ($UnitOfWork->GetExecutedProcedures() as $Procedure) {
         $Transaction->Execute($this->MapProcedure($Procedure));
     }
     foreach ($UnitOfWork->GetDiscardenceDataGroups() as $EntityType => $DiscardedIdentityGroup) {
         $EntityRelationalMap = $this->EntityRelationMaps[$EntityType];
         $ResultRows = $this->MapEntityDataToTransaction($UnitOfWork, $Transaction, $EntityRelationalMap, $DiscardedIdentityGroup);
         foreach ($ResultRows as $ResultRow) {
             $Transaction->DiscardAll($ResultRow->GetPrimaryKeys());
         }
     }
     foreach ($UnitOfWork->GetDiscardedCriteria() as $DiscardedCriterion) {
         $Transaction->DiscardWhere($this->MapObjectCriterion($DiscardedCriterion));
     }
 }
Beispiel #5
0
 /**
  * @expectedException \Storm\Core\Object\ObjectException
  */
 public function testUnmappedCriterionIsDisallowed()
 {
     $Criterion = $this->getMock(self::CoreObjectNamespace . 'ICriterion');
     $Criterion->expects($this->any())->method('GetEntityType')->will($this->returnValue(__CLASS__));
     $this->UnitOfWork->DiscardWhere($Criterion);
 }
 public final function Discard(Object\UnitOfWork $UnitOfWork, $ParentEntity)
 {
     $Domain = $UnitOfWork->GetDomain();
     list($CurrentValue, $HasOriginalValue, $OriginalValue) = $this->GetEntityRelationshipData($ParentEntity);
     return $this->DiscardRelationshipChanges($Domain, $UnitOfWork, $ParentEntity, $CurrentValue, $HasOriginalValue, $OriginalValue);
 }
Beispiel #7
0
 /**
  * Constructs a unit of work instance containing the supplied operations to commit.
  * 
  * @param object[] $EntitiesToPersist
  * @param IProcedure[] $ProceduresToExecute
  * @param object[] $EntitiesToDiscard
  * @param ICriterion[] $CriterionToDiscard
  * @return UnitOfWork The constructed unit of work
  */
 public final function BuildUnitOfWork(array $EntitiesToPersist = [], array $ProceduresToExecute = [], array $EntitiesToDiscard = [], array $CriterionToDiscard = [])
 {
     $UnitOfWork = new UnitOfWork($this);
     foreach ($EntitiesToPersist as $Entity) {
         $UnitOfWork->Persist($Entity);
     }
     foreach ($ProceduresToExecute as $Procedure) {
         $UnitOfWork->Execute($Procedure);
     }
     foreach ($EntitiesToDiscard as $Entity) {
         $UnitOfWork->Discard($Entity);
     }
     foreach ($CriterionToDiscard as $Criterion) {
         $UnitOfWork->DiscardWhere($Criterion);
     }
     return $UnitOfWork;
 }