skip to Main Content

I want to print a variable with PersistentCollection Type (products)
I made a for loop but I got this error

Object of class AppEntityProduct could not be converted to string

The variable products with type PersitentCollection, I want to print it in twig

My Approvement entity


<?php

namespace AppEntity;

use AppRepositoryApprovementRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

#[ORMEntity(repositoryClass: ApprovementRepository::class)]
class Approvement
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;

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

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

    /**
     * @ORMColumn(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;
    }
}


My Twig

{% 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 %}

I want to print a variable with type PeristentCollection

2

Answers


  1. With {{ item }} in twig, you are trying to print the object. You have to print any property in this object like you did with:

      <td>{{approvement.nameclient}}</td>
      <td>{{approvement.number}}</td>
    
    Login or Signup to reply.
  2. As mentioned in a previous answer {{ item }} need a stringable variable, in your case the Product entity is an object and twig do not know how to render a string with it.

    You could add a __toString method to your entity like this:

    public function __toString(): string
    {
        return $this->yourNameProperty;
    }
    

    And twig would be able to use it to turn your object into a string.

    Otherwise the best thing to do is use your object properties & method.

    Example:

      <td>
          {% for item in approvement.products %}
              {{ item.myMethodOrPublicProperty }}
          {% endfor %}
      </td>
    

    Or if you want to have every values separated by a comma, you could do something like this:

    {% for item in approvement.products %}
        {{ item.name }}
        {{ not loop.last ? ',' }}
    {% endfor %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search