Firstly, I am guessing that in your twig code type.webPath that should be media.webPath? As it doesn't make sense for webPath to be an attribute of the media type?
The database structure is fine. However, you cannot directly follow the relationship typeMedia.media to get the results for a single structure. The typeMedia.media relationship will always give you all media of that type (for all structures), regardless of how you navigated to the typeMedia. Furthermore, as long as your outer loop is iterating over a set of media, it is only possible to output by mediaType from twig if that set of media is already ordered by mediaType. This is not the case by default when using structure.media.
There are various ways you could do this. One way would be to add a method to your Structure entity class to get the media by type. The advantage of this approach is that it enables you to get the media by type for a structure from anywhere in your code - repository, service, controller or twig. For example:
In your Structure entity class:
public function getMediaByTypeName()
{
$mediaByType = array();
foreach ($this->media as $aMedia)
{
$typeName = $aMedia->getTypeMedia()->getName();
if (!array_key_exists($typeName, $mediaByType)) $mediaByType[$typeName] = array();
$mediaByType[$typeName][] = $aMedia;
}
return $mediaByType;
}
In your twig:
{% for typeName, media in structure.mediaByTypeName %}
<h3>{{ typeName }}</h3>
<hr/>
{% for aMedia in media %}
<img src="{{ aMedia.webPath | imagine_filter('thumb', true) }}" />
{% endfor %}
{% endfor %}