如何循环数组集合以显示在字符串中

问题描述

我想在 Twig显示我在控制器中使用 ParamConverter 获得的“策略”的“强度”字符串列表(存在一对多关系,“强度”是策略作为数组集合)。

我可以让它在控制器中工作以在我的树枝文件获取正确的数据(通过转储验证),但 Twig 中没有显示任何内容(是的,数据已写入数据库,我检查了它不是正面问题)。

所以我阅读了很多关于同一问题的主题,很多人说我必须循环数组集合才能以字符串形式显示它,我尝试过但无法使其工作。

控制器文件

/**
* @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
    ]);
}

树枝文件

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

我的转储显示内容

dump display

我也在这样的循环中尝试了一个循环,但我得到了相同的结果,但没有显示任何内容,并且我的转储数据相同:

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

{{ dump(strategy.strenghts)}}

编辑,这是我的两个实体:

策略:

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;
    }
}

诊断力:

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;
    }
}

解决方法

通过自动装配,您的 $strategy 参数应该已经是与 $paramStrategy 相同的实体,无需使用存储库。

因此,在 twig 中,您应该只需要以下内容:

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

这应该与 php 中的结果相同:

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