如何打印持久集合变量twig

问题描述 投票:0回答:1

我想打印一个具有 PersistentCollection 类型的变量(产品) 我做了一个 for 循环,但出现了这个错误 App\Entity\Product 类的对象无法转换为字符串

PersitentCollection 类型的变量 products,我想在 twig 中打印它

我的审批实体


<?php

namespace App\Entity;

use App\Repository\ApprovementRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ApprovementRepository::class)]
class Approvement
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    /**
     * @var Collection<int, Product>
     */
    #[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'approvement')]
    private Collection $products;

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

    /**
     * @ORM\Column(type="integer", nullable=true) 
     */
    private ?int $number = null;

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

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

    /**
     * @return Collection<int, Product>
     */
    public function getProducts(): Collection
    {
        return $this->products;
    }

    public function addProduct(Product $product): static
    {
        if (!$this->products->contains($product)) {
            $this->products->add($product);
            $product->setApprovement($this);
        }

        return $this;
    }

    public function removeProduct(Product $product): static
    {
        if ($this->products->removeElement($product)) {
            // set the owning side to null (unless already changed)
            if ($product->getApprovement() === $this) {
                $product->setApprovement(null);
            }
        }

        return $this;
    }

    public function getNameClient(): ?string
    {
        return $this->name_client;
    }

    public function setNameClient(string $name_client): static
    {
        $this->name_client = $name_client;

        return $this;
    }

    public function getNumber(): ?int
    {
        return $this->number;
    }

    public function setNumber(int $number): static
    {
        $this->number = $number;

        return $this;
    }
}


我的树枝

{% extends 'admin/admin.html.twig' %}

{% block title %}Hello HomeController!{% endblock %}

{% block stylesheets %}
    <link rel="stylesheet" href={{asset('styles/css/approvements.css')}}>
{% endblock %}
{% block main %}
<div class="add">
<a href="{{path('approvement_add')}}" class="btn btn-primary">Ajouter une Vente</a>
</div>
<table class="table">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Produits</th>
      <th scope="col">Nom Client</th>
      <th scope="col">Numéro</th>
    </tr>
  </thead>
  <tbody>
    {% for approvement in approvements %}
    <tr>
      <th scope="row">{{approvement.id}}</th>
      <td>{% for item in approvement.products %}
          {{ item }}
          {% endfor %}
      
      </td>
      <td>{{approvement.nameclient}}</td>
      <td>{{approvement.number}}</td>
      </tr>
    {% endfor %}
          <a href="{{path('view_cart')}}">Voir panier</a>

    
  </tbody>
</table>

{% endblock %}

我想打印一个 PeristentCollection 类型的变量

php mysql symfony
1个回答
0
投票

使用 twig 中的

{{ item }}
,您正在尝试打印该对象。您必须打印该对象中的任何属性,就像您所做的那样:

  <td>{{approvement.nameclient}}</td>
  <td>{{approvement.number}}</td>
© www.soinside.com 2019 - 2024. All rights reserved.