private function persistRelationshipsTraversal(Entity $metadata, $entity, Reader $reader, $local_id)
{
$relationships = $metadata->getRelationships();
$is_proxy = $entity instanceof OrmProxyInterface;
foreach ($relationships as $relationship) {
$key = $this->getRelationshipKey($relationship, $local_id);
$value = $reader->getPropertyValue($relationship->getName());
// Skip relationship rules:
// If the entity is not a proxy (i.e. a new entity) we still must allow for the scenario in which a new
// entity is created over the top of an existing entity (same ID), as such, we still need to check every
// relationship attached to the entity
if (!$this->entity_manager->getMaintenanceMode() && $is_proxy) {
/** @var OrmProxyInterface $entity */
// Check if we can skip the update
if (!$entity->isRelativeModified($relationship->getName())) {
// Looks like we can skip, but check inverted sort indices first -
if ($relationship->getInversedBy()) {
$inverse_relationship = $this->invertRelationship($relationship);
if ($inverse_relationship->getSortableBy()) {
// The inverse relationship has sortable columns, we need to check for local property
// changes that might have impacted this -
list(, , $maintain) = $this->getRelationshipDeltas($key, $relationship, $value);
$this->updateMaintainedRelationshipSortIndices($inverse_relationship, $maintain, $reader, $local_id);
}
}
// Nothing else to update on this relationship
continue;
}
}
// This condition allows NEW (not a proxy) entities that have NOT set a relationship to inherit existing
// relationships which could be useful if the relationship was set by a foreign entity
// See: docs/RaceConditions.md
if ($is_proxy || $value || $this->entity_manager->getMaintenanceMode()) {
$this->persistForwardRelationship($relationship, $key, $value);
if (count($relationship->getSortableBy())) {
$this->persistForwardSortIndices($relationship, $local_id, $value);
}
// Modify the inversed relationships
if ($relationship->getInversedBy()) {
$this->persistInversedRelationship($relationship, $key, $value, $local_id, $reader);
} else {
$this->persistRefs($relationship, $key, $value, $local_id);
}
}
}
}