Witam mam taka zagwozdkę.

Struktura tabeli w uproszczeniu.
item_type, item_type_id, ad_id
TypeImage, 1, 1
TypeImage, 2, 1
TypeDescription, 1, 1

Gdzie
item_type (enum) to Encja np ItemTypeImage, ItemTypeDescription
item_type_id to id encji
ad_id to id "właściciela" dla tych wpisów.
PRIMARY_KEY item_type, item_type_id

i teraz

kontroler
  1. $items = $repository->getItems($entity, $this->container->get('templating'));
  2. return $this->render('AppMainBundle:Ad:view.html.twig', [
  3. 'entity' => $entity,
  4. 'items' => $items
  5. ]);


repository
  1. public function getItems(Ad $entity, TwigEngine $templating) {
  2. $em = $this->getEntityManager();
  3. $queryBuilder = $em->createQueryBuilder();
  4. $queryBuilder->from('AppMainBundle:AdItem', 'ai');
  5. $queryBuilder->select('ai');
  6. $queryBuilder->where($queryBuilder->expr()->eq('ai.ad', $entity->getId()));
  7. $queryBuilder->andWhere($queryBuilder->expr()->eq('ai.active', true));
  8. $queryBuilder->orderBy('ai.weight', 'ASC');
  9.  
  10. foreach ($queryBuilder->getQuery()->getResult() as $adItem) {
  11. try {
  12. $typeRepository = $em->getRepository('AppMainBundle:' . $adItem->getItemType());
  13. $typeEntity = $typeRepository->find($adItem->getItemTypeId());
  14. if ($typeEntity) {
  15. yield new AdItemModel($adItem, $typeEntity, $templating);
  16. }
  17. } catch (\Exception $exc) {
  18. dump($exc->getMessage());
  19. }
  20. }
  21. }


AdItemModel
  1. class AdItemModel {
  2.  
  3. private $adItem;
  4. private $typeEntity;
  5. private $templating;
  6.  
  7. public final function __construct(AdItem $adItem, ItemTypeInterface $typeEntity, TwigEngine $templating) {
  8. $this->adItem = $adItem;
  9. $this->typeEntity = $typeEntity;
  10. $this->templating = $templating;
  11. }
  12.  
  13. public final function getAdItem() {
  14. return $this->adItem;
  15. }
  16.  
  17. public final function getTypeEntity() {
  18. return $this->typeEntity;
  19. }
  20.  
  21. public final function view() {
  22. try {
  23. return $this->templating->render('AppMainBundle:AdItemType:' . $this->getAdItem()->getItemType() . '.html.twig', [
  24. 'adItem' => $this->getAdItem(),
  25. 'typeEntity' => $this->getTypeEntity(),
  26. ]);
  27. } catch (\Exception $exc) {
  28. return $exc->getMessage() . PHP_EOL;
  29. }
  30. }
  31.  
  32. }


view.html.twig
  1. {% for item in items %}
  2. <div class="item {{ item.adItem.getItemTypeClass }}">
  3. {{ item.view }}
  4. </div>
  5. {% endfor %}


Generalnie wykonując item.view chce wypluć templatkę encji i w przypadku gdy zdefiniuję nowy typ przechwycić to do wyjątku jeśli templatka nie istnieje lub opcjonalnie poślę tam templatkę domyślną ale wynik === nic, null, zero, czarna dziura biggrin.gif

Natomiast w przypadku braku templatki wynik to

Kod
Unable to find template "AppMainBundle:AdItemType:ItemTypeText.html.twig" (looked into: C:\WTServer\WWW\projekt\app/Resources/views, C:\WTServer\WWW\projekt\vendor\symfony\symfony\src\Symfony\Bridge\Twig/Resources/views/Form, C:\WTServer\WWW\projekt\vendor\knplabs\knp-menu\src\Knp\Menu/Resources/views).


Dziwi mnie to że nie szuka w AppMainBundle to raz i nie wpadłem jak ten problem rozwiązać.

Do moderacji. Przepraszam temat do usunięcia znalazłem błąd chwile po dodaniu wpisu.