如何循环数组集合以在字符串中显示
How to loop an array collection to display in strings
我想在 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
]);
}
Twig 文件:
{% for paramStrategy in paramStrategy %}
{{ paramStrategy.strenghts }}
{% endfor %}
{{ dump(paramStrategy.strenghts)}}
我的转储显示的内容:
我也试过在这样的循环中循环,但我得到了相同的结果,但没有显示任何内容,并且与我的转储中的数据相同:
{% 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";
}
我想在 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
]);
}
Twig 文件:
{% for paramStrategy in paramStrategy %}
{{ paramStrategy.strenghts }}
{% endfor %}
{{ dump(paramStrategy.strenghts)}}
我的转储显示的内容:
我也试过在这样的循环中循环,但我得到了相同的结果,但没有显示任何内容,并且与我的转储中的数据相同:
{% 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";
}