/**
  * Sets the entity relationship metadata from the metadata mapping.
  *
  * @param   Metadata\Interfaces\RelationshipInterface   $metadata
  * @param   array                                       $relMapping
  * @return  Metadata\Interfaces\RelationshipInterface
  * @throws  RuntimeException If the related entity type was not found.
  */
 protected function setRelationships(Metadata\Interfaces\RelationshipInterface $metadata, array $relMapping)
 {
     foreach ($relMapping as $key => $mapping) {
         if (!is_array($mapping)) {
             $mapping = ['type' => null, 'entity' => null];
         }
         if (!isset($mapping['type'])) {
             $mapping['type'] = null;
         }
         if (!isset($mapping['entity'])) {
             $mapping['entity'] = null;
         }
         if (!isset($mapping['search'])) {
             $mapping['search'] = [];
         }
         $relationship = new Metadata\RelationshipMetadata($key, $mapping['type'], $mapping['entity'], $this->isMixin($metadata));
         if (isset($mapping['description'])) {
             $relationship->description = $mapping['description'];
         }
         if (isset($mapping['inverse'])) {
             $relationship->isInverse = true;
             if (isset($mapping['field'])) {
                 $relationship->inverseField = $mapping['field'];
             }
         }
         $path = $this->getFilePath('model', $mapping['entity']);
         $relatedEntityMapping = $this->getMapping('model', $mapping['entity'], $path);
         if (isset($relatedEntityMapping['entity']['polymorphic'])) {
             $relationship->setPolymorphic(true);
             $relationship->ownedTypes = $this->getOwnedTypes($mapping['entity']);
         }
         if (isset($mapping['search']['store'])) {
             $relationship->setSearchProperty(true);
         }
         $metadata->addRelationship($relationship);
     }
     return $metadata;
 }