1
votes

I have a problem with properties in (probably) Twig. I have controller in Symfony where getCategories(), getWords(), getTranslations() methods (from Doctrine) return the objects (relations). Every property in the controller is an array because I call findAll() method (from Doctrine again) which returns the array. Finally I return all the properties from controller to view (Twig file) where I try display the results by Twig for loop.

The problem is the Twig loop only iterates over flashcards property (I know why ;)) and I have no idea how to make many-properties iterating. I'd like the loop to iterate over all properties returned by the controller.

In the controller foreach loop I tried update the flashcards array with new associative keys such as: category, word and translation so that all the results returned by Doctrine (including relations) are stored in one flashcards property but then Symfony throws exceptions.

I wondered if create one array in the controller to which I would push the flashcards, cateogry, word and translation arrays and then return this one array to the view but I don't think this is good practice.

Here's the controller method code:

public function showAllCards()
{
    $flashcards = $this->getDoctrine()->getRepository(Flashcards::class)
        ->findAll();

     foreach ($flashcards as $flashcard) {
        $category = $flashcard->getCategories()->getName();
        $word = $flashcard->getWords()->getWord();
        $translation = $flashcard->getTranslations()->getWord();
    }

      return $this->render('try_me/index.html.twig', [
        'flashcards' => $flashcards,
        'category' => $category,
        'word' => $word,
        'translation' => $translation
    ]);
}

Here's the Twig loop code:

{% for flashcard in flashcards %}

  {{ word }}
  <br>
  {{ flashcard.pronunciation }}
  <br>
  {{ flashcard.exampleSentence }}
  <br>
  {{ category }}
  <br>
  {{ translation }}
  <br>

{% endfor %}

I tried to execute the following controller code...

public function showMeAll()
{
    $flashcards = $this->getDoctrine()->getRepository(Flashcards::class)
        ->findAll();

     foreach ($flashcards as $flashcard) {
        $flashcards['categories'] = $flashcard->getCategories()->getName();
        $flashcards['words'] = $flashcard->getWords()->getWord();
        $flashcards['translations'] = $flashcard->getTranslations()->getWord();
    }

      return $this->render('try_me/index.html.twig', [
        'flashcards' => $flashcards,
    ]);
}

...with the following Twig loop...

{% for flashcard in flashcards %}

  {{ flashcard.words }}
  <br>
  {{ flashcard.pronunciation }}
  <br>
  {{ flashcard.exampleSentence }}
  <br>
  {{ flashcard.categories }}
  <br>
  {{ flashcard.translations }}
  <br>

{% endfor %}

...but then Symfony says:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies__CG__\App\Entity\Words could not be converted to string").

Could you give me some tips how to solve this problem, please? I'd like the Twig loop to iterates over many properties (flashcard, word, category, translation). Or write if there's a better solution, please.

Thank you in advance for every answer!

1

1 Answers

0
votes

According to your snippets, I'm guessing you want something like the following:

{% for flashcard in flashcards %}
    {% for word in flashcard.getWords() %}
        {{ word }}<br />
    {% endfor %}
    {{ flashcard.getPronunciation() }}<br>
    {{ flashcard.getExampleSentence() }}<br>
    {% for category in flashcard.getCategories()() %}
         {{ category.getName() }}<br />
    {% endfor %}
    {% for translation in flashcard.getTranslations() %}
         {{ translation.getWord() }}<br />
    {% endfor %}
{% endfor %}

Have a look at this section of the documentation. Basically if you had foo.bar, twig will test if bar is a public property of foo and if not test if there is a public getter, getBar, to fetch bar.


Some sidenotes in both of your loops, the values category, word and translation will only hold the last value of your flashcards, because you are overwriting the value each time.