0

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;
    }
}
2
  • Do the entities in paramStrategy.strenghts have a __toString() method? Otherwise, what property holds the string you are trying to render? Commented Feb 9, 2021 at 23:27
  • Yes the Strategy entity already have a __toString() method for its title property. I edited my question to add the two entities Strategy and DiagnosticForce. Commented Feb 9, 2021 at 23:40

1 Answer 1

0

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";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this answer, it solved my problem. I had already tried it but without writing a public function __toString() in my DiagnosticForce entity. So your answer is the right one, plus the addition of a public function __toString() { return $this->strenght; } in the DiagnosticForce entity. (and you are right about the autowiring, getting the repository via $paramStrategy was not needed). Thanks a lot Arleigh Hix !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.