Storm\Core\Object\UnitOfWork::PersistRelationships PHP Method

PersistRelationships() public method

Persist an entity's relationships to the unit of work.
public PersistRelationships ( object $Entity ) : void
$Entity object The entity to persist
return void
    public function PersistRelationships($Entity)
    {
        $Hash = spl_object_hash($Entity);
        if (isset($this->PersistenceData[$Hash])) {
            return;
        }
        $this->Domain->PersistRelationships($this, $Entity);
    }

Usage Example

Esempio n. 1
0
 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;
 }