Search code examples
twigsymfony5arraycollection

How to loop an array collection to display in strings


I want to display in Twig a string list of "strenghts" for a "strategy" that I get in the controller with ParamConverter (there is a one to many relation, and "strenghts" is an attribute in strategy as an array collection).

I could make it work in the controller to get the right data in my twig file (verified with a dump), but nothing is displayed in Twig (and yes data is written in database, and I checked it's not a front problem).

So I have read many topics about the same problem, and a lot of them say that I have to loop on the array collection to be able to display it in strings, which I tried but couldn't make it work.

Controller file :

/**
* @Route("fr/strategy/content/{title}", name="frContentStrategy")
*/
public function displayStrategy(Strategy $strategy): Response
{
    $paramStrategy = $this->getDoctrine()->getRepository(Strategy::class)->find($strategy);
    return $this->render('content_strategy/contentStrategy.html.twig', [
        "strategy" => $strategy,
        "paramStrategy" => $paramStrategy
    ]);
}

Twig file :

{% for paramStrategy in paramStrategy %}
    {{ paramStrategy.strenghts }}
{% endfor %}
        
{{ dump(paramStrategy.strenghts)}}

What my dump displays :

dump display

I also tried a loop inside a loop like this but I get the same result with nothing displayed, and the same data with my dump :

{% for paramStrategy in paramStrategy %}
    {{ paramStrategy.strenghts }}
    {% for strategy in paramStrategy.strenghts %}
        {{ strategy.strenghts }}
    {% endfor %}
{% endfor %}

{{ dump(strategy.strenghts)}}

Edit, here are my two entities:

Strategy :

class Strategy
{

    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @ORM\ManyToOne(targetEntity=User::class, inversedBy="strategies")
     */
    private $user;

    /**
     * @ORM\OneToMany(targetEntity=DiagnosticForce::class, mappedBy="strategy")
     */
    private $strenghts;

    public function __construct()
    {
        $this->strenghts = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

    /**
     * @return Collection|DiagnosticForce[]
     */
    public function getStrenghts(): Collection
    {
        return $this->strenghts;
    }

    public function addStrenght(DiagnosticForce $strenght): self
    {
        if (!$this->strenghts->contains($strenght)) {
            $this->strenghts[] = $strenght;
            $strenght->setStrategy($this);
        }

        return $this;
    }

    public function removeStrenght(DiagnosticForce $strenght): self
    {
        if ($this->strenghts->removeElement($strenght)) {
            // set the owning side to null (unless already changed)
            if ($strenght->getStrategy() === $this) {
                $strenght->setStrategy(null);
            }
        }

        return $this;
    }

    public function __toString()
    {
        return $this->title;
    }
}

DiagnosticForce :

class DiagnosticForce
{

    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $strenght;

    /**
     * @ORM\ManyToOne(targetEntity=Strategy::class, inversedBy="strenghts")
     */
    private $strategy;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getStrenght(): ?string
    {
        return $this->strenght;
    }

    public function setStrenght(?string $strenght): self
    {
        $this->strenght = $strenght;

        return $this;
    }

    public function getStrategy(): ?Strategy
    {
        return $this->strategy;
    }

    public function setStrategy(?Strategy $strategy): self
    {
        $this->strategy = $strategy;

        return $this;
    }
}

Solution

  • With autowiring your $strategy parameter should already be the same entity as $paramStrategy, no need to use the repository.

    So, in twig you should just need the following:

    {% for strenght in strategy.strenghts %}
      {{ strenght }}
    {% endfor %}
    

    This should have the same result as the following in php:

    foreach ($strategy->getStrenghts() as $strenght){
      echo "\n$strenght\n";
    }