code: public function addAttachedEntity(IdentifiableEntityInterface $entity, $key = null)
      {
          $className = get_class($entity);
          
          if (! isset($key)) {
              $key = $className;
          }
          
          $reference = [
              'repository' => $className
          ];
          $entityId = $entity->getId();
          
          // check if entity is not persisted
          if (! $entityId) {
              // persist entity & retrieve its ID
              $this->repositories->getRepository($className)->store($entity);
              $entityId = $entity->getId();
          }
          
          $reference['id'] = $entityId;
          $this->references[$key] = $reference;
          
          return $this;
      }
docs: Adds an $entity using an optional $key.
  If $key is not provided then $entity's FQCN will be used as a key
  Any existing $entity with the same $key will be replaced.
  
  @param IdentifiableEntityInterface $entity
  @param string $key
  @return AttachableEntityManager